*Memo:
- My post explains a list (2).
- My post explains a list (3).
- My post explains a list (4).
- My post explains a list (5).
- My post explains a list (6).
- My post explains list functions (1).
- My post explains a list comprehension.
- My post explains a list shallow and deep copy.
- My post explains a tuple (1).
- My post explains a set (1).
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a string (1).
- My post explains a bytes (1).
- My post explains a bytearray (1).
- My post explains a range (1).
A list:
- is the ordered mutable(unhashable) collection of zero or more elements whose type is
list:- Ordered means that the order of each element in a list is kept so it guarantees that the order is always the same unless changed.
- Mutable(Unhashable) means the elements of a list can be changed.
- allows duplicated elements.
- can have any types of elements.
- can be used with len() to get the length.
- is
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnotkeyword. - can be checked if a specific element is and isn't in the list with
inkeyword and withnotandinkeyword respectively. - can be checked if the list is and isn't referred to by two variables with
iskeyword and withisandnotkeyword respectively:- List literals with
iskeyword and withisandnotkeyword don't get warnings respectively.
- List literals with
- and other list can be checked if all the elements in:
- them are and aren't the same with
==and!=respectively. - the list are in other list with
<=. - other list are in the list with
>=. - the list and other elements are in other list with
<. - other list and other elements are in the list with
>.
- them are and aren't the same with
- and other list cannot be checked if they have and don't have their common elements with
bool()and&and withnotkeyword and&respectively. - can be enlarged with
*and a number. - and other lists can be concatenated with
+. - and other list cannot return:
- all the elements in them with
'|'(Union: A ∪ B). - their common elements with
'&'(Intersection: A ∩ B). - the elements in the list which aren't in other list with
'-'(Difference: A - B). - the elements in either the list or other list but not both with
'^'(Symmetric Difference: A Δ B).
- all the elements in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, function and*but not with**. - can be created by
[]with or without',', by list() with or without an iterable and by a list comprehension:- For
list(), the words type conversion are also suitable in addition to the word creation.
- For
- cannot be big because it gets
MemoryError. - can be read by indexing and slicing.
- can be changed by indexing, slicing and a del statement.
- can be continuously used through multiple variables.
- can be shallow-copied by list.copy(), copy.copy(),
list()and slicing. - can be deep-copied by copy.deepcopy().
Be careful, a big list gets MemoryError.
MemoryError.[] with or without ',' can create a list as shown below:
v = [] # Empty 1D list
v = [0, 1, 2, 3, 4] # 1D list
v = [0, 1, 2, 0, 1, 2] # 1D list
v = [0, 1, 2, 3, [4, 5, 6, 7]] # 2D list
v = [[0, 1, 2, 3], [4, 5, 6, 7]] # 2D list
v = [[0, 1, 2, 3], [[4, 5], [6, 7]]] # 3D list
v = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] # 3D list
# No error
v = [0, 0.0, 0.0+0.0j, False]
v = [1, 1.0, 1.0+0.0j, True]
v = ['A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True,
[2, 3], (2, 3), {2, 3}, frozenset({2, 3}),
{'A':'a'}, range(2, 3), iter([2, 3])]
print(len([0, 1, 2, 3, 4]))
print(bool([0]))
print(bool([[]]))
print(bool([]))
print(not [0])
print(not [[]])
print(not [])
print('A' in ['A', ['B', 'C']])
print('A' not in ['A', ['B', 'C']])
print([0, 1, 2] is [0, 1, 2]) # No warning
print([0, 1, 2] is not [0, 1, 2]) # No warning
print([0, 1, 2] == [0, 1, 2])
print([0, 1, 2] != [0, 1, 2])
print([0, 1, 2] <= [0, 1, 2])
print([0, 1, 2] >= [0, 1, 2])
print([0, 1, 2] < [0, 1, 2])
print([0, 1, 2] > [0, 1, 2])
v = [0, 1, 2, 3, 4] * 3
v = ['A', 'B', 'C', 'D', 'E'] * 3
v = [] * 3
v = [0, 1, 2] + [[3, 4]] + [[[5, 6, 7, 8]]]
for x in [0, 1, 2, 3, 4]: pass
for x in [[0, 1, 2, 3], [4, 5, 6, 7]]: pass
for x in [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]: pass
v1, v2, v3 = [0, 1, 2]
v1, *v2, v3 = [0, 1, 2, 3, 4, 5]
for v1, v2, v3 in [[0, 1, 2], [3, 4, 5]]: pass
for v1, *v2, v3 in [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]: pass
print(*[0, 1], 2, *[3, 4, *[5]])
print([*[0, 1], 2, *[3, 4, *[5]]])
v = [x**2 for x in [0, 1, 2, 3, 4, 5, 6, 7]]
v = [[y**2 for y in x] for x in [[0, 1, 2, 3], [4, 5, 6, 7]]]
v = [[[z**2 for z in y] for y in x] for x in [[[0, 1], [2, 3]],
[[4, 5], [6, 7]]]]
# No error
print(bool([0, 1, 2] & [1, 3]))
print(not ([0, 1, 2] & [1, 3]))
print([0, 4] | [0, 2, 4] | [0, 1, 3, 4])
print([0, 4] & [0, 2, 4] & [0, 1, 3, 4])
print([0, 4] - [0, 2, 4] - [0, 1, 3, 4])
print([0, 1, 2, 3] ^ [0, 2, 4])
v = [0, 1, 2, 3, 4] * 1000000000
v = list(range(1000000000))
v = [x for x in range(1000000000)]
# Error
A list is the ordered mutable(unhashable) collection of zero or more elements whose type is list as shown below:
v = [0, 1, 2, 3, 4]
print(v)
# [0, 1, 2, 3, 4]
print(type(v))
# <class 'list'>
v[1] = 'X'
v[3] = 'Y'
print(v)
# [0, 'X', 2, 'Y', 4]
v = [] # Empty list
print(v)
# []
A list allows duplicated elements as shown below:
v = [0, 1, 2, 0, 1, 2]
print(v)
# [0, 1, 2, 0, 1, 2]
v = [0, 0.0, 0.0+0.0j, False]
print(v)
# [0, 0.0, 0j, False]
v = [1, 1.0, 1.0+0.0j, True]
print(v)
# [1, 1.0, (1+0j), True]
A list can have any types of elements as shown below:
v = ['A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True,
[2, 3], (2, 3), {2, 3}, frozenset({2, 3}),
{'A':'a'}, range(2, 3), iter([2, 3])]
print(v)
# ['A', b'A', bytearray(b'A'), 2, 2.3, (2.3+4.5j), True,
# [2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A': 'a'},
# range(2, 3), <list_iterator object at 0x000001F3BA8A6560>]
A list can be used with len() to get the length as shown below:
v = [0, 1, 2, 3, 4]
print(len(v))
# 5
Top comments (0)