6 minute read

Introduction

In Python, a list and a set are both used to store collections of items, but they have some key differences in terms of functionality and performance.

In this article, we will explore the main differences between lists and sets in Python, how to initialize them, adding, removing elements and iterating over them, as well as show examples of how to use them in different situations.

Lists

A list in Python is an ordered collection of items, where each item can be of any type, including other lists. Lists are created by enclosing a comma-separated sequence of items in square brackets ([]). Here is an example of how to create a list in Python:

numbers = [1, 2, 3, 4, 5]
words = ["apple", "banana", "cherry"]
mixed = [1, "apple", 3.14, [1, 2, 3]]

Lists are ordered, meaning that the items in a list have a specific order, and they can be accessed using indexing.

Additionally, lists are mutable, meaning that their elements can be changed after they are created. Here is an example of how to access and change elements in a list:

numbers = [1, 2, 3, 4, 5]
print(numbers[2]) # Output: 3
numbers[2] = 6
print(numbers) # Output: [1, 2, 6, 4, 5]

You can also add elements to a list using the append() method, and remove elements using the remove() method. Here is an example of how to add and remove elements in a list:

numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # Output: [1, 2, 3, 4, 5, 6]
numbers.remove(4)
print(numbers) # Output: [1, 2, 3, 5, 6]

Notice how we are passing the element that we wish to remove to the remove() method. Alternatively, you may pass the index which we want to remove to the pop() method like this:

numbers = [99, 12, 41, 51]
numbers.pop(1) # Index 1 (12)
print(numbers) # Output: [99, 41, 51]

By passing 1 to the pop() method, we are removing the element on the 1st position of the list.

You can also iterate over the elements in a list using a for loop:

for number in numbers:
    print(number)

Sets

A set in Python is an unordered collection of unique items, where each item can be of any type, including other sets. Sets are created by enclosing a comma-separated sequence of items in curly braces ({}). Here is an example of how to create a set in Python:

numbers = {1, 2, 3, 4, 5}
words = {"apple", "banana", "cherry"}
mixed = {1, "apple", 3.14, {1, 2, 3}}

It is important to note that they can only hold unique items, so if we try to add a duplicate element:

numbers = {1, 2, 3, 4, 5}
print(numbers) # Output: {1, 2, 3, 4, 5}

numbers.add(5)
print(numbers) # Output: {1, 2, 3, 4, 5}

Notice how the element 5 did not get added to the set again, since it can only contain unique elements.

You can also create a set from a list by passing a list to the set() constructor. Here is an example:

numbers_list = [1, 2, 3, 4, 5]
numbers_set = set(numbers_list)

Sets are unordered, meaning that the items in a set have no specific order, and they can’t be accessed using indexing.

It is important to note that, just like lists, sets are also mutable, meaning that their elements can be changed after they are created. Here is an example of how to add and remove elements in a set:

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

Output: {1, 2, 3, 4, 5, 6}

numbers.remove(4)
print(numbers) # Output: {1, 2, 3, 5, 6}

Note that we use the add() method to add elements to a set, unlike lists (which use append()). You can also iterate over the elements in a set using a for loop:

for number in numbers:
    print(number)

Differences

Ordered lists vs unordered sets

The main difference between lists and sets is that lists are ordered collections of items, while sets are unordered collections of unique items.

This means that lists have a specific order for their elements, and elements in a list can be accessed using indexing. Sets, on the other hand, do not have a specific order for their elements, and elements in a set cannot be accessed using indexing.

List duplicates vs set uniqueness

Another difference is that lists allow duplicate elements, while sets do not allow duplicates. If you try to add a duplicate element to a set, it will not be added, and the set will remain unchanged. This is specially useful when we want to make sure that elements are unique (without needing to use a dictionary).

Performance and time complexity

In terms of performance, sets are generally faster than lists when it comes to checking if an item is in the collection or not. This is because, unlike lists which use O(n) linear search, sets use a technique called hashing to quickly check if an item is in the collection. Therefore, sets allows us to access data in O(1) time complexity.

Conclusion

In conclusion, lists and sets in Python are both used to store collections of items, but they have some key differences in terms of functionality and performance. Lists are ordered collections of items, while sets are unordered collections of unique items. Lists allow duplicate elements, while sets do not.

Additionally, it is essential to note that sets are generally faster than lists when it comes to checking if an item is in the collection or not.