Sobes.tech
Middle+

What is the difference between list, tuple, and set?

sobes.tech AI

Answer from AI

In Python, list, tuple, and set are different types of collections with different properties:

  • list — an ordered, mutable list of elements. Allows duplicates and elements of different types. Can be modified (adding, removing, changing elements).

  • tuple — an ordered, immutable set of elements. Like list, it can contain duplicates and different types, but cannot be changed after creation. Used when immutability and data protection are needed.

  • set — an unordered collection of unique elements. Does not allow duplicates, and elements must be hashable. Used for fast lookup, removing duplicates.

Example:

my_list = [1, 2, 2, 3]
my_tuple = (1, 2, 2, 3)
my_set = {1, 2, 2, 3}  # will be {1, 2, 3}

my_list[0] = 10  # works
# my_tuple[0] = 10  # error