Python List vs Set
List vs Set in Python
In this tutorial, you will learn the differences between a list and set in Python language.
We shall go through the aspects of list and set, and discuss about the difference between them in detail.
1. Order of items : list vs set
list
Items in a list are ordered, whereas, items in a set or not ordered.
Which means that each item in a list has a specific index and the index starts at 0 from the beginning of the list, and increments in steps of one for the subsequent items.
my_list = ["apple", "banana", "cherry"]
print(my_list[0]) # apple
print(my_list[1]) # banana
print(my_list[2]) # cherry
Run it as many number of times as possible. The output remains same, as the order of items is fixed.
set
Whereas in a set, there is no index for items. When we try to pick an item from the set, we cannot know which item we would get from the set.
my_set = {"apple", "banana", "cherry"}
set_iterator = iter(my_set)
print(next(set_iterator))
print(next(set_iterator))
print(next(set_iterator))
The order of items in not guaranteed. Run the above program twice, and you will see the difference in the order of items printed to output.
2. Unique Values : list vs set
list
A list can contain duplicate values.
my_list = ["apple", "banana", "apple", "cherry"]
print(my_list)
In the above list, the item "apple" has occurred twice. This is allowed for a list.
set
A set can contain only unique values. No duplicates are allowed.
my_set = {"apple", "banana", "cherry"}
Even if we try to specify a duplicate item for a set, it takes only unique values. For example, in the following program, we specified "apple" twice in the list. But if you print the items in the set to output, you could observe that there is only one "apple" in the set.
Python Program
my_set = {"apple", "banana", "apple", "cherry"}
print(my_set)
Output
{'banana', 'apple', 'cherry'}
3. Built-in function : list vs set
list
Python list() built-in function is used to initialize a list. It can create a new list with some initial values, or convert an iterable to list.
# Create list from initial values
my_list = list(["apple", "banana", "cherry"])
print(my_list)
# Create list from a tuple
my_tuple = (25, "apple", 3.14)
my_list = list(my_tuple)
print(my_list)
set
Python set() built-in function is used to initialize a set in Python. It can create a new set with some initial values, or convert an iterable to a set.
# Create set from initial values
my_set = set({"apple", "banana", "cherry"})
print(my_set)
# Create set from a tuple
my_tuple = (25, "apple", 3.14)
my_set = set(my_tuple)
print(my_set)
4. Initialization : list vs set
list
To initialize a list with values, we use comma separated values enclosed in square brackets.
my_list = ["apple", "banana", "cherry"]
set
To initialize a set with values, we use comma separated values enclosed in curly brackets.
my_set = {"apple", "banana", "cherry"}
Summary
In this tutorial, we have seen the differences between a list and set in Python language, with detailed explanation for each of the difference.