Python: List Methods

, , …,

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

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
β„“[i] = vset ith element to v
β„“[i] = vset ith element to v
β„“[i:j] = β„“2replace i to j elements by the contents of the β„“2
del β„“[i]remove ith element.
del β„“[i:j]remove the elements between i to j (not including j). Same as β„“[i:j] = []
del β„“[i:j:k]remove the elements between i to j (not including j), with step k.
β„“[i:j:k] = β„“2replace elements β„“[i:j:k] by content of β„“2. (β„“2 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
β„“.pop()remove last element and return it.
β„“.pop(i)remove ith element and return it. Same as v = β„“[i]; del β„“[i]; return v

Add to List, Append to List (Push)

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

# list append and extend example

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β€Ί) example. β€Ήvβ€Ί can be a atom or list. If list, it is flattened one level.

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
β„“.index(v)return the index of first occurrence of v. Error if v is not in β„“.
β„“.index(v, i)start at index i
β„“.index(v, i, j)end at index j, not including j
β„“.count(v)return the number of occurrences of v
β„“.remove(v)remove first occurrence of v. Error if v is not in β„“. Same as del β„“[β„“.index(v)]
finding item
SyntaxMeaning
e in β„“return True if element e is in the list β„“, else, False. γ€”β˜›Β Python's Context Dependent Syntax Soup: γ€Œβ€¦ in …」 And γ€Œβ€¦ not in …」〕
# list count example
print [1,2,3,2,7,2].count(2)    # 3

Sort οΌ† Reverse

sort οΌ† reverse
SyntaxMeaning
β„“.reverse()reverses the items of β„“ in place.
β„“.sort()sort the items of β„“ in place
β„“.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.
β„“.sort(key=f)sort using f to extract a β€œkey”. f takes 1 arg. Result is compared using cmp(). γ€”β˜›Β Python οΌ† Perl: Sort Matrix, Object, Math of Sorting〕
β„“.sort(key=f, reverse=true/false)β—‡

5. Built-in Types β€” Python v2.7.6 documentation #mutable-sequence-types

blog comments powered by Disqus