DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

List in Python (2)

Buy Me a Coffee

*Memo:

A non-empty list and empty list are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not keyword respectively.
print(bool([0]))  # list
print(bool([[]])) # list(Empty list)
# True

print(bool([])) # Empty list
# False
Enter fullscreen mode Exit fullscreen mode
print(not [0])  # list
print(not [[]]) # list(Empty list)
# False

print(not []) # Empty list
# True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode
v = ['A', 'B', 'C', 'D', 'E'] * 3

print(v)
# ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E']
Enter fullscreen mode Exit fullscreen mode
v = [] * 3

print(v)
# []
Enter fullscreen mode Exit fullscreen mode

<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]]
Enter fullscreen mode Exit fullscreen mode
v = [['A', 'B', 'C', 'D', 'E']] * 3

print(v)
# [['A', 'B', 'C', 'D', 'E'],
#  ['A', 'B', 'C', 'D', 'E'],
#  ['A', 'B', 'C', 'D', 'E']]
Enter fullscreen mode Exit fullscreen mode
v = [[]] * 3

print(v)
# [[], [], []]
Enter fullscreen mode Exit fullscreen mode

<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]]]
Enter fullscreen mode Exit fullscreen mode
v = [[['A', 'B', 'C', 'D', 'E']]] * 3

print(v)
# [[['A', 'B', 'C', 'D', 'E']],
#  [['A', 'B', 'C', 'D', 'E']],
#  [['A', 'B', 'C', 'D', 'E']]]
Enter fullscreen mode Exit fullscreen mode
v = [[[]]] * 3

print(v)
# [[[]], [[]], [[]]]
Enter fullscreen mode Exit fullscreen mode

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]]]
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode
print([0, 4] & [0, 2, 4] & [0, 1, 3, 4])
# TypeError: unsupported operand type(s) for &: 'list' and 'list'
Enter fullscreen mode Exit fullscreen mode
print([0, 4] - [0, 2, 4] - [0, 1, 3, 4])
# TypeError: unsupported operand type(s) for -: 'list' and 'list'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)