DEV Community

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

Posted on • Edited 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 respectively.
# Non-empty list
print(bool([0]))  
print(bool([[]]))
# True

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

# Empty list
print(not [])
# 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 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
Enter fullscreen mode Exit fullscreen mode
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
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 and is not respectively as shown below:

*Memo:

  • List literals with is and is not 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]) # 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
Enter fullscreen mode Exit fullscreen mode

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.
    • E.g. [0, 1, 2] > [0, 1, 3] is False, comparing 2 > 3.
    • E.g. [0, 1, 2] > [0, 1] is True, comparing 2 > an absent element(lower value than the other).
    • E.g. [0, 1, 2] > [0, 3] is False, comparing 1 > 3.
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
# 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
Enter fullscreen mode Exit fullscreen mode
# 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'
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)