List in Python (2)

Buy Me a Coffee☕

*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:

True and False, checking them wit…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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
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.
  • if the list is and isn't referred to by two variables with is keyword and with is and not keyword respectively.
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
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

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 list 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'


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-10-23T22:36:17+00:00) List in Python (2). Retrieved from https://www.scien.cx/2025/10/23/list-in-python-2-2/

MLA
" » List in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Thursday October 23, 2025, https://www.scien.cx/2025/10/23/list-in-python-2-2/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Thursday October 23, 2025 » List in Python (2)., viewed ,<https://www.scien.cx/2025/10/23/list-in-python-2-2/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » List in Python (2). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/23/list-in-python-2-2/
CHICAGO
" » List in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/10/23/list-in-python-2-2/
IEEE
" » List in Python (2)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/10/23/list-in-python-2-2/. [Accessed: ]
rf:citation
» List in Python (2) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/10/23/list-in-python-2-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.