*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 withnotrespectively.
# Non-empty list
print(bool([0]))
print(bool([[]]))
# True
# Empty list
print(bool([]))
# False
# Non-empty list
print(not [0])
print(not [[]])
# False
# Empty list
print(not [])
# True
A list can be checked if a specific element is and isn't in the list with in and not in respectively as shown below:
v = ['A', ['B', 'C']]
print('A' in v)
print(['B', 'C'] in v)
# True
print('B' in v)
print('C' in v)
print(['A'] in v)
print(['B'] in v)
print(['C'] in v)
print(['A', ['B', 'C']] in v)
print([] in v)
# False
v = ['A', ['B', 'C']]
print('A' not in v)
print(['B', 'C'] not in v)
# False
print('B' not in v)
print('C' not in v)
print(['A'] not in v)
print(['B'] not in v)
print(['C'] not in v)
print(['A', ['B', 'C']] not in v)
print([] not in v)
# True
A list can be checked if the list is and isn't referred to by two variables with is and is not respectively as shown below:
*Memo:
- List literals with
isandis notdon'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]) # False
print([0, 1, 2] is [0, 1]) # False
print([0, 1, 2] is not [0, 1, 2]) # True
print([0, 1, 2] is not [0, 1]) # True
A list and other list can be checked if:
- all the elements in them are and aren't equal with
==and!=respectively:-
==and!=can also check if types of values are and aren't the same respectively.
-
- the list is greater than other list with
>. - the list is greater than or equal to other list with
>=. - the list is less than other list with
<. - the list is less than or equal to other list with
<=.
*Memo:
- Lexicographical comparison is used with their lists:
- Lexicographical comparison:
- compares each element in their lists from their 1st elements one by one:
- To compare the characters in two strings or the bytes in two bytes objects or two bytearrays, their Unicode points are used.
- finishes the comparison:
- just after the current comparison which has a difference between two elements in their lists is done.
- or just after the current comparison of the element and absent element and vice versa in their lists is done:
- An absent element is evaluated to a lower value than the other.
- or if both of their lists are exhausted.
- compares each element in their lists from their 1st elements one by one:
- E.g.
[0, 1, 2] > [0, 1, 3]isFalse, comparing2 > 3. - E.g.
[0, 1, 2] > [0, 1]isTrue, comparing2 > an absent element(lower value than the other). - E.g.
[0, 1, 2] > [0, 3]isFalse, comparing1 > 3.
- Lexicographical comparison:
v = [0, 1, 2]
print(v == [0, 1, 2]) # True
print(v == [2, 1, 0]) # False
print(v == [0, 1, 3]) # False
print(v == [0, 1, -3]) # False
print(v == [0, 1]) # False
print(v == [0, 3]) # False
print(v == [0, -3]) # False
print(v == [0, 1, 2, 3]) # False
print(v == [0, 1, 3, 4]) # False
print(v == [0, 1, -3, -4]) # False
print(v == []) # False
# tuple
print(v == (0, 1, 2)) # False
v = [0, 1, 2]
print(v != [0, 1, 2]) # False
print(v != [2, 1, 0]) # True
print(v != [0, 1, 3]) # True
print(v != [0, 1, -3]) # True
print(v != [0, 1]) # True
print(v != [0, 3]) # True
print(v != [0, -3]) # True
print(v != [0, 1, 2, 3]) # True
print(v != [0, 1, 3, 4]) # True
print(v != [0, 1, -3, -4]) # True
print(v != []) # True
# tuple
print(v != (0, 1, 2)) # True
v = [0, 1, 2]
print(v > [0, 1, 2]) # False
print(v > [2, 1, 0]) # False
print(v > [0, 1, 3]) # False
print(v > [0, 1, -3]) # True
print(v > [0, 1]) # True
print(v > [0, 3]) # False
print(v > [0, -3]) # True
print(v > [0, 1, 2, 3]) # False
print(v > [0, 1, 3, 4]) # False
print(v > [0, 1, -3, -4]) # True
print(v > []) # True
v = [0, 1, 2]
print(v >= [0, 1, 2]) # True
print(v >= [2, 1, 0]) # False
print(v >= [0, 1, 3]) # False
print(v >= [0, 1, -3]) # True
print(v >= [0, 1]) # True
print(v >= [0, 3]) # False
print(v >= [0, -3]) # True
print(v >= [0, 1, 2, 3]) # False
print(v >= [0, 1, 3, 4]) # False
print(v >= [0, 1, -3, -4]) # True
print(v >= []) # True
v = [0, 1, 2]
print(v < [0, 1, 2]) # False
print(v < [2, 1, 0]) # True
print(v < [0, 1, 3]) # True
print(v < [0, 1, -3]) # False
print(v < [0, 1]) # False
print(v < [0, 3]) # True
print(v < [0, -3]) # False
print(v < [0, 1, 2, 3]) # True
print(v < [0, 1, 3, 4]) # True
print(v < [0, 1, -3, -4]) # False
print(v < []) # False
v = [0, 1, 2]
print(v <= [0, 1, 2]) # True
print(v <= [2, 1, 0]) # True
print(v <= [0, 1, 3]) # True
print(v <= [0, 1, -3]) # False
print(v <= [0, 1]) # False
print(v <= [0, 3]) # True
print(v <= [0, -3]) # False
print(v <= [0, 1, 2, 3]) # True
print(v <= [0, 1, 3, 4]) # True
print(v <= [0, 1, -3, -4]) # False
print(v <= []) # False
# Equivalent
print(['0', '1', '2'] == ['0', '1', '2']) # True
print(['012'] == ['012']) # True
print(['01', '2'] == ['01', '2']) # True
print(['0', '12'] == ['0', '12']) # True
print([48, 49, 50] == [48, 49, 50]) # True
print(['0', '1', '2'] == [48, 49, 50]) # False
# Equivalent
print(['01', '2'] > ['0', '2']) # True
print([['0', '1'], '2'] > [['0'], '2']) # True
print([[48, 49], 50] > [[48], 50]) # True
print([['0', '1'], '2'] > [[48], 50])
# TypeError: '>' not supported between instances of 'str' and 'list'
A list and other list cannot be checked if they have and don't have their common elements with bool() and & and with not and & respectively as shown below:
v = [0, 1, 2]
print(bool(v & [1, 3]))
print(not (v & [1, 3]))
# TypeError: unsupported operand type(s) for &: 'list' and 'list'
Top comments (0)