Difference between set and list in python

Set vs list in Python

A better way to understand the key difference between sets and lists in Python is to understand the definition regardless of language or syntax. Generally speaking, a set is a mathematical concept which refers to a collection of distinct objects (in math they are called elements). Distinct means there are no duplicates and a collection means there is no order for individual elements. On the other hand, a list is essentially an array. An array can have duplicate elements and each element has an order or an index within the array.

That is the main difference, let us now be more specific…

Set properties in Python

  • A set is a collection. It can contain objects of different types
  • A set is unordered. You can not refer to individual elements using an index
  • A set does not support slicing. Later we will provide a list slicing example
  • A set is iterable. For more information about iterators you may check the following article
  • A set is mutable. You can modify the content of a set
  • A set has no duplicates. At any point in time, all elements in the set are unique. In Python, if you try to add an element that already exists, Python will enforce the uniqueness requirement
  • A set cannot contain mutable elements such as lists but it can contain tuples as elements because tuples are immutable
  • A set is faster to lookup items as it internally uses hashing techniques

List properties in Python

  • Like a set, a list can contain objects of different types
  • A list is ordered
  • A list supports slicing. See slicing example below
  • Like a set, a list is iterable
  • Like a set, a list is mutable
  • A list can contain other lists
  • A list is slightly faster to iterate over items

So, the main differences are: order, uniqueness of elements and performance. You can find below a couple of examples demonstrating how to use sets and list in Python:

Set examples

List examples

When to use a set?

  • If you want to test element membership. A set data structure is optimized for that (hashing). Of course you can do that using a dictionary but a set gives us that out of the box.
  • If you want to remove duplicates. For example find all the words used in a text file.
  • Depending on your purpose, mathematical set operations can be used ready out of the box such as intersection, difference, union, etc

When to use lists?

  • If order is important and sorting is needed
  • Slightly faster than sets when iterating over items

Summary

Here is a tabular summary of differences between sets and lists in Python

SetList
Can contain objects of different typesCan contain objects of different types
UnorderedOrdered
Does not support slicingSupports slicing
IterableIterable
MutableMutable
No duplicatesDuplicates
Cannot contain mutablesCan contain other lists
Faster to lookup itemsSlightly faster to iterate over items

References

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *