*Memo:
- My post explains a list (1).
- My post explains a list (3).
- My post explains a list (4).
- My post explains a list (5).
- My post explains a list (6).
A non-empty list and empty list are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
print(bool([0])) # list
print(bool([[]])) # list(Empty list)
# True
print(bool([])) # Empty list
# False
print(not [0]) # list
print(not [[]]) # list(Empty list)
# False
print(not []) # Empty list
# True
A list can be checked if a specific element is and isn't in the list with in keyword and with not and in keyword respectively as shown below:
v = ['A', ['B', 'C']]
print('A' in v)
print(['B', 'C'] in v)
# True
print('a' in v)
print('B' in v)
print('C' in v)
print(['B'] in v)
print(['C'] in v)
print(['A', ['B', 'C']] in v)
# False
v = ['A', ['B', 'C']]
print('A' not in v)
print(['B', 'C'] not in v)
# False
print('a' not in v)
print('B' not in v)
print('C' not in v)
print(['B'] not in v)
print(['C'] not in v)
print(['A', ['B', 'C']] not in v)
# True
A list can be checked if the list is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:
*Memo:
List literals with is keyword and with is and not keyword don't get warnings respectively.
v1 = [0, 1, 2]
v2 = [0, 1, 2]
v3 = v1
print(v1 is v2) # False
print(v1 is v3) # True
print(v1 is not v2) # True
print(v1 is not v3) # False
print([0, 1, 2] is [0, 1, 2]) # True
print([0, 1, 2] is [0, 1]) # False
print([0, 1, 2] is not [0, 1, 2]) # False
print([0, 1, 2] is not [0, 1]) # True
A list and other list can be checked if every element in:
- them is and isn't the same with
==and!=respectively. - the list is in other list with
<=. - other list is in the list with
>=. - the list and other elements are in other list with
<. - other list and other elements are in the list with
>.
v = [0, 1, 2]
print(v == [0, 1, 2]) # True
print(v == [0, 1]) # False
print(v == [0, 3]) # False
print(v == [0, 1, 2, 3]) # False
print(v == [0, 1, 3, 4]) # False
print(v == []) # False
v = [0, 1, 2]
print(v != [0, 1, 2]) # False
print(v != [0, 1]) # True
print(v != [0, 3]) # True
print(v != [0, 1, 2, 3]) # True
print(v != [0, 1, 3, 4]) # True
print(v != []) # True
v = [0, 1, 2]
print(v <= [0, 1, 2]) # True
print(v <= [0, 1]) # False
print(v <= [0, 3]) # True
print(v <= [0, 1, 2, 3]) # True
print(v <= [0, 1, 3, 4]) # True
print(v <= []) # False
v = [0, 1, 2]
print(v >= [0, 1, 2]) # True
print(v >= [0, 1]) # True
print(v >= [0, 3]) # False
print(v >= [0, 1, 2, 3]) # False
print(v >= [0, 1, 3, 4]) # False
print(v >= []) # True
v = [0, 1, 2]
print(v < [0, 1, 2]) # False
print(v < [0, 1]) # False
print(v < [0, 3]) # True
print(v < [0, 1, 2, 3]) # True
print(v < [0, 1, 3, 4]) # True
print(v < []) # False
v = [0, 1, 2]
print(v > [0, 1, 2]) # False
print(v > [0, 1]) # True
print(v > [0, 3]) # False
print(v > [0, 1, 2, 3]) # False
print(v > [0, 1, 3, 4]) # False
print(v > []) # True
A list and other list cannot be checked if they have and don't have common elements with bool() and & and with not keyword and & respectively as shown below:
v = [0, 1, 2]
print(bool(v & [3, 4]))
print(not (v & [3, 4]))
# TypeError: unsupported operand type(s) for &: 'list' and 'list'
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)
# [[[]], [[]], [[]]]
A list and other lists can be concatenated with + as shown below:
v = [0, 1, 2] + [[3, 4]] + [[[5, 6, 7, 8]]]
print(v)
# [0, 1, 2, [3, 4], [[5, 6, 7, 8]]]
A list and other lists 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 lists with
'-'(Difference: A - B).
print([0, 4] | [0, 2, 4] | [0, 1, 3, 4])
# TypeError: unsupported operand type(s) for |: 'list' and 'list'
print([0, 4] & [0, 2, 4] & [0, 1, 3, 4])
# TypeError: unsupported operand type(s) for &: 'list' and 'list'
print([0, 4] - [0, 2, 4] - [0, 1, 3, 4])
# TypeError: unsupported operand type(s) for -: 'list' and 'list'
A list and other list cannot return the elements in the list but not in other list or the elements not in the list but in other list with '^' (Symmetric Difference: A Δ B) as shown below:
print([0, 1, 2, 3] ^ [0, 2, 4])
# TypeError: unsupported operand type(s) for ^: 'list' and 'list'
Top comments (0)