What is the difference between lists, tuples and sets in Python?

  • How do these objects in Python collect and store data?
  • How are the three similar?

Lists, tuples and sets are built-in data structures in Python.

List

  • Ordered collection of data points in square brackets [], separated by commas.
  • Can store any type any data type or combination of data types.
  • Lists are mutable.
  • Elements in a list need not be unique and can be repeated.


Tuple

  • Ordered collection of data points in parenthesis (), separated by commas.
  • Can store any type any data type or combination of data types.
  • Tuples are immutable and it is one of the identifying features of tuples.
  • Elements in a list need not be unique and can be repeated.


Set

  • Unordered collection of data points in curly brackets {}, separated by commas.
  • Sets are mutable, however, only immutable objects can be stored in it.
  • Elements in a set are unique.
  • As, no order is associated to sets, this data type does not support slicing or indexing.



It is to be noted that as tuples are immutable, any functions that change a collection of data points such as .append(), .remove(), .pop() etc. are not applicable on tuples.


The following instances will help one differentiate between the three even better.

Initializing/Creating an empty list, an empty tuple and an empty set

Input:

foo = []

bar = ()

baz = set()

It is to be noted that one can not create an empty set using {} as baz = {} would create an empty dictionary rather than an empty set.

Adding elements

  • Adding elements to a list using .append() method.

Input:

foo = [1,2,3,4]
foo.append(5)
print(foo)

Output:

[1, 2, 3, 4, 5]

On trying to add elements to both sets and tuples using .append() method, Python will throw an AttributeError.

  • Adding elements to a set using .add() method.

Input:

baz = {1,2,3,4}
baz.add(5)
print(baz)

Output:

{1, 2, 3, 4, 5}

Removing elements

  • Removing elements from a list and a set using .remove() method.

Input:

foo = [1,2,3,4]
foo.remove(2)
print(foo)baz = {1,2,3,4}
baz.remove(3)
print(baz)

Output:

[1, 3, 4]
{1, 2, 4}
  • Removing elements from a set using .discard() method.

.remove() method will thrown a KeyError if we try to remove an element that is not in the list. So we have .discard() method as it will not throw KeyError and return the set as it is. Also, lists do not have the attribute .discard().

Input:

baz = {1,2,3,4}
baz.discard(5)
print(baz)

Output:

{1, 2, 3, 4}

Combining two objects of the same kind

Two lists or two tuples can be combined using + while two sets can be combined using .union() method.

Input:

foo1 = [1,2]
foo2 = [3,4]
print(foo1 + foo2)

bar1 = (1,2)
bar2 = (3,4)
print(bar1 + bar2)

baz1 = {1,2}
baz2 = {3,4}
print(baz1.union(baz2))

Output:

[1, 2, 3, 4]
(1, 2, 3, 4)
{1, 2, 3, 4}

Sorting

As sets are unordered, they do not have an attribute for sorting.

  • Sorting lists using the .sort() method.

Input:

foo = [3,1,2,4]
foo.sort()
print(foo)

Output;

[1, 2, 3, 4]
  • Sorting tuples using the sorted() function.

Tuples being immutable can not be sorted. We can use sorted() function as it returns a sorted list of any iterable object.

Input:

bar = (3,1,2,4)
print(sorted(bar))

Output:

[1, 2, 3, 4]

Converting one data type to another.

  • Lists can be converted into tuples using the function tuple() and into sets using set(). It is to be noted the function set() will not take duplicate values.
  • Tuples can be converted into lists using the function list() and into sets using set().
  • Similarly, sets can be converted into lists using the function list() and into tuples using the function tuple().



The methods and operations listed here are not exhaustive. there are more such methods and operations that can work on these objects.One should know that a full understanding of data structures is highly important as one can then have a better grasp at any programming language.

3 Likes