Python: List Methods

By Xah Lee. Date: . Last updated: .

This page is a complete list of Python list methods. For a basic tutorial on list, see: Python: List Basics

Note: index start at 0. 0th item meas first item. Ending index does not include itself.

print ["a","b","c"][0:1]  # ['a']

All list methods modifies the list in-place.

List Elements Extraction, Slice, Generic Add/Remove/Set List Elements

generic add/remove/set
SyntaxMeaning
lst[i] = vset ith element to v
lst[i] = vset ith element to v
lst[i:j] = lst2replace i to j elements by the contents of the lst2
del lst[i]remove ith element.
del lst[i:j]remove the elements between i to j (not including j). Same as lst[i:j] = []
del lst[i:j:k]remove the elements between i to j (not including j), with step k.
lst[i:j:k] = lst2replace elements lst[i:j:k] by content of lst2. (lst2 must have same number of elements)

List slice assignment example:

# -*- coding: utf-8 -*-
# python

# list slice assignment example

e = range(30)
e[0:30:3] = ["a"] * 10
print e

# ['a', 1, 2, 'a', 4, 5, 'a', 7, 8, 'a', 10, 11, 'a', 13, 14, 'a', 16, 17, 'a', 19, 20, 'a', 22, 23, 'a', 25, 26, 'a', 28, 29]

List Pop, Remove First Element

pop
SyntaxMeaning
lst.pop()remove last element and return it.
lst.pop(i)remove ith element and return it. Same as v = lst[i]; del lst[i]; return v

Add to List, Append to List (Push)

add to list
SyntaxMeaning
lst.append(v)append v to the end. Same as lst[len(lst):len(lst)] = [v]
lst.extend(v)append v to the end, but flatten v one level if its a list. Same as lst[len(lst):len(lst)] = v
lst.insert(i, v)insert at i. Same as lst[i:i] = [v]
# -*- coding: utf-8 -*-
# python

# list append and extend

e = range(3)
e.append(["a",[8]]) # [0, 1, 2, ['a', [8]]]
print e

u = range(3)
u.extend(["a",[8]]) # [0, 1, 2, 'a', [8]]
print u

in list.extend(v), v can be a atom or list. If list, it is flattened one level.

# -*- coding: utf-8 -*-
# python

# ‹list›.extend(‹v›)

u = range(3)
v = range(3)
u.extend(["b"]) # [0, 1, 2, 'b']
v.extend("b")   # [0, 1, 2, 'b']
print u
print v

Finding Item

finding item
SyntaxMeaning
lst.index(v)return the index of first occurrence of v. Error if v is not in lst.
lst.index(v, i)start at index i
lst.index(v, i, j)end at index j, not including j
lst.count(v)return the number of occurrences of v
lst.remove(v)remove first occurrence of v. Error if v is not in lst. Same as del lst[lst.index(v)]
finding item
SyntaxMeaning
e in lstreturn True if element e is in the list lst, else, False. [see Python's Context Dependent Syntax Soup: “in” And “not in”]
# list count example
print [1,2,3,2,7,2].count(2)    # 3

Sort and Reverse

sort and reverse
SyntaxMeaning
lst.reverse()reverses the items of lst in place.
lst.sort()sort the items of lst in place
lst.sort(f)using predicate function f to sort. f takes 2 args, and return {1, 0, -1}, but any int works. The sign indicates order.
lst.sort(key=f)sort using f to extract a “key”. f takes 1 arg. Result is compared using cmp(). [see Python: Sort]
lst.sort(key=f, reverse=true/false)

Python Lisp Topic

  1. List Basics
  2. Loop Thru List
  3. Map Function to List
  4. List Comprehension
  5. List Methods

If you have a question, put $5 at patreon and message me.

Python by Example

  1. Python Basics
  2. Print Version String
  3. Builtin Help
  4. Quote String
  5. String Operations
  6. String Methods
  7. Format String
  8. True, False
  9. if then else
  10. for, while, Loops
  11. List Basics
  12. Loop Thru List
  13. Map Function to List
  14. List Comprehension
  15. List Methods
  16. Dictionary
  17. Loop Thru Dict
  18. Dict Methods
  19. Function
  20. Class
  21. List Modules
  22. Write a Module
  23. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. Manipulate Path
  4. Process Unicode
  5. Convert File Encoding
  6. Find Replace in dir
  7. Find Replace by Regex
  8. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Complex Numbers
  7. Sort
  8. Copy Nested List
  9. Tuple vs List
  10. Sets, Union, Intersection
  11. Closure in Python 2
  12. Decorator
  13. Append String in Loop
  14. Timing f timeit
  15. Keyword Arg Default Value Unstable
  16. Check Page Load Size
  17. Thumbnail Generation

Python 3

  1. Python 3 Basics
  2. Print Version String
  3. Quoting String
  4. String Operations
  5. Format String
  6. Operators
  7. Object, ID, Type
  8. Traverse Directory
  9. Sort List, Matrix, Object
  10. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  11. Python 3 Closure
  12. Python 2 and 3 Difference