Sobes.tech
Senior

Name the main difference between a list, a tuple, and a set.

sobes.tech AI

Answer from AI

Main differences between list, tuple, and set in Python:

  • List — an ordered, mutable collection of elements. Allows duplicates and can be modified.

  • Tuple — an ordered, immutable collection. Elements cannot be changed after creation, but duplicates are allowed.

  • Set — an unordered collection of unique elements. Does not allow duplicates and does not guarantee order.

Example:

lst = [1, 2, 2, 3]      # list with duplicates
tpl = (1, 2, 2, 3)      # tuple with duplicates
st = {1, 2, 2, 3}       # set, duplicates automatically removed

print(lst)  # [1, 2, 2, 3]
print(tpl)  # (1, 2, 2, 3)
print(st)   # {1, 2, 3}

Thus, the choice of structure depends on the need for mutability, order, and element uniqueness.

Name the main difference between a list, a tuple, and… - sobes.tech