*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 list functions (1).
- My post explains a list comprehension.
- My post explains the shallow copy and deep copy of a list.
- My post explains a tuple.
- My post explains a set and the set with copy.
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a string.
- My post explains a bytes.
- My post explains a bytearray.
- 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 (even with different types).
- can have any types of elements.
- can be used with len() to get the length.
- is
False
if it's empty. - can be checked if a specific element is in the list with
in
keyword. - can be checked if the list is referred to by two variables with
is
keyword. - can be enlarged with
*
and a number. - cannot be huge because it gets
MemoryError
. - can be iterated with a
for
statement. - can be created by
'[]'
with or without','
, list() with or without an iterable or a list comprehension:- For
list()
, the words type conversion are also suitable in addition to the word creation.
- For
- can be read or changed by indexing and slicing.
- can be changed by indexing, slicing and a del statement.
- can be unpacked with an assignment and
for
statement, function and*
but not with**
. - can be continuously used through multiple variables.
- can be shallow-copied with list.copy(), copy.copy(),
list()
and slicing. - can be deep-copied with copy.deepcopy().
'[]'
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]))
v = [0, 1, 2, 3, 4] * 3
v = ['A', 'B', 'C', 'D', 'E'] * 3
v = [] * 3
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
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]]]]
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]]])
# No error
v = [0, 1, 2, 3, 4] * 1000000000
v = list(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] = 'A'
v[3] = 'B'
print(v)
# [0, 'A', 2, 'B', 4]
v = [] # Empty list
print(v)
# []
A list allows duplicated elements (even with different types) 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
An empty list is False
as shown below:
print(bool([])) # Empty list
# False
print(bool([0])) # list
print(bool([[]])) # list(Empty list)
# True
A list can be checked if a specific element is in the list with in
keyword as shown below:
v = ['A', 'B', ['C', 'D']]
print('B' in v)
# True
print(['C', 'D'] in v)
# True
print('b' in v)
# False
A list can be enlarged with *
and a number as shown below:
<1D list>:
v = [0, 1, 2, 3, 4] * 3
print(v)
# [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
v = ['A', 'B', 'C', 'D', 'E'] * 3
print(v)
# ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E']
v = [] * 3
print(v)
# []
<2D list>:
v = [[0, 1, 2, 3, 4]] * 3
print(v)
# [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
v = [['A', 'B', 'C', 'D', 'E']] * 3
print(v)
# [['A', 'B', 'C', 'D', 'E'],
# ['A', 'B', 'C', 'D', 'E'],
# ['A', 'B', 'C', 'D', 'E']]
v = [[]] * 3
print(v)
# [[], [], []]
<3D list>:
v = [[[0, 1, 2, 3, 4]]] * 3
print(v)
# [[[0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4]]]
v = [[['A', 'B', 'C', 'D', 'E']]] * 3
print(v)
# [[['A', 'B', 'C', 'D', 'E']],
# [['A', 'B', 'C', 'D', 'E']],
# [['A', 'B', 'C', 'D', 'E']]]
v = [[[]]] * 3
print(v)
# [[[]], [[]], [[]]]
Be careful, a huge list gets MemoryError
as shown below:
v = [0, 1, 2, 3, 4] * 1000000000
# MemoryError
v = range(1000000000)
print(list(v))
# MemoryError
A list can be iterated with a for
statement as shown below:
<1D list>:
for x in [0, 1, 2, 3, 4]:
print(x)
# 0
# 1
# 2
# 3
# 4
<2D list>:
for x in [[0, 1, 2, 3], [4, 5, 6, 7]]:
for y in x:
print(y)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
<3D list>:
for x in [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]:
for y in x:
for z in y:
print(z)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
Top comments (0)