python

  • list() new empty list
  • list(iterable) new list initialized from iterable’s items

    The constructor builds a list whose items are the same and
    in the same order as iterable’s items.

iterable may be either a sequence, a container that supports iteration,
or an iterator object. If iterable is already a list, a copy is made and returned,
similar to iterable[:]. e.g. list('abc') => ['a', 'b', 'c'],
list(range(2)) => [0, 1], list([0, 1]) => [0, 1].

Create

  • l.copy() # New in Python3

    Return a shallow copy of l.

  • l.append(p_object)

    Append object to the end of l.

  • l.insert(index, p_object)

    Insert object before index.

  • l.extend(iterable)

    Extend list by appending elements from iterable.

Delete

  • l.pop([index])

    Remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

  • l.remove(value)

    Remove first occurrence of value.
    Raises ValueError if the value is not present.

  • l.clear() # New in Python3

    Return a shallow copy of l.

Retrieve

  • l.count(value)

    Return number of occurrences of value.

  • l.index(value, [start[, stop]])

    Return first index of value.

Update

  • l.reverse()

    Reverse IN PLACE. e.g.

    1
    2
    3
    4
    l = [0, 1, 2]
    l.reverse() # No list return, but reverse in place
    print(l)
    [2, 1, 0]
  • l.sort([key, reverse])

    Sorts the list l in place.

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments).

Error: l.sort(lambda x: x == 2), l.sort(lambda x: x == 2, True)

True: l.sort(key=lambda x: x == 2), l.sort(key=lambda x: x == 2, reverse=True)

key, specifies a function of one argument that is used to extract a
comparision key from each list element (e.g. key=str.lower).

The smaller of the key function return value, the corresponding item is more forward.
And the True indicate int 1, False indicate int 0.

1
2
3
4
5
6
7
l = [5, 1, 2, 4, 0]
l.sort(key=lambda x: x == 2)
print(l)
[5, 1, 4, 0, 2] # 2 => True(1); others => False(0)
l.sort(key=lambda x: x)
print(l)
[0, 1, 2, 4, 5]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
"""
def append(self, p_object):
""" L.append(object) -> None -- append object to end """
pass

def clear(self): # New in Python3
""" L.clear() -> None -- remove all items from L """
pass

def copy(self): # New in Python3
""" L.copy() -> list -- a shallow copy of L """
return []

def count(self, value):
""" L.count(value) -> integer -- return number of occurrences of value """
return 0

def extend(self, iterable):
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass

def index(self, value, start=None, stop=None):
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0

def insert(self, index, p_object):
""" L.insert(index, object) -- insert object before index """
pass

def pop(self, index=None):
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass

def remove(self, value):
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass

def reverse(self):
""" L.reverse() -- reverse *IN PLACE* """
pass

def sort(self, key=None, reverse=False):
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass

def __add__(self, *args, **kwargs):
""" Return self+value. """
pass

def __contains__(self, *args, **kwargs):
""" Return key in self. """
pass

def __delitem__(self, *args, **kwargs):
""" Delete self[key]. """
pass

def __eq__(self, *args, **kwargs):
""" Return self==value. """
pass

def __getattribute__(self, *args, **kwargs):
""" Return getattr(self, name). """
pass

def __getitem__(self, y):
""" x.__getitem__(y) <==> x[y] """
pass

def __ge__(self, *args, **kwargs):
""" Return self>=value. """
pass

def __gt__(self, *args, **kwargs):
""" Return self>value. """
pass

def __iadd__(self, *args, **kwargs):
""" Implement self+=value. """
pass

def __imul__(self, *args, **kwargs):
""" Implement self*=value. """
pass

def __init__(self, seq=()):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
# (copied from class doc)
"""
pass

def __iter__(self, *args, **kwargs):
""" Implement iter(self). """
pass

def __len__(self, *args, **kwargs):
""" Return len(self). """
pass

def __le__(self, *args, **kwargs):
""" Return self<=value. """
pass

def __lt__(self, *args, **kwargs):
""" Return self<value. """
pass

def __mul__(self, *args, **kwargs):
""" Return self*value.n """
pass

@staticmethod
def __new__(*args, **kwargs):
""" Create and return a new object. See help(type) for accurate signature. """
pass

def __ne__(self, *args, **kwargs):
""" Return self!=value. """
pass

def __repr__(self, *args, **kwargs):
""" Return repr(self). """
pass

def __reversed__(self):
""" L.__reversed__() -- return a reverse iterator over the list """
pass

def __rmul__(self, *args, **kwargs):
""" Return self*value. """
pass

def __setitem__(self, *args, **kwargs):
""" Set self[key] to value. """
pass

def __sizeof__(self):
""" L.__sizeof__() -- size of L in memory, in bytes """
pass

__hash__ = None