Differences and Similarities between Python Lists, Sets and Tuples.
Lists, Sets and Tuples are data structures in python. They store values (like strings, numbers, even other lists, sets and tuples!). Their values are comma separated and enclosed in brackets.
Differences
- Perhaps the most popular difference is the type of brackets each data structure uses. Lists use square brackets, Sets use curly brackets and Tuples use the familiar round brackets.
>>> myList = [1, 2, 3]
>>> type(myList)
<class 'list'>
>>> myTuple = (1, 2, 3)
>>> type(myTuple)
<class 'tuple'>
>>> mySet = {1, 2, 3}
>>> type(mySet)
<class 'set'>
The type function tells the class of the argument passed into it. The type of brackets you enclose your values in pretty much defines what type of data structure you are creating.
2. Lists and Tuples are ordered data structures while Sets are unordered. In simple terms, if I were to create a set data structure, and I were to print out the results, the result would not be in the order I originally typed it in.
>>> fruits = {'apple', 'banana', 'cherry', 'date'}
>>> print(fruits)
{'cherry', 'banana', 'apple', 'date'}
Since sets are not ordered, items cannot be accessed in a set by indexing. Lists and Tuples do not change their order at all.
3. Lists and Sets are mutable, Tuples are immutable. This means that you cannot add new elements to a tuple. This can be seen by the absence of methods that can be used add new items to a tuple [e.g append(), extend(), add()]. Once a tuple is created, it cannot be changed or modified. There are ways around this problem though😏. One of such ways is to change the data structure from a tuple to a list, modify the list, then turn the list back to a tuple.
>>> small_numbers = (0, 1, 2, 3)
>>> edit_tuple = list(small_numbers)
>>> edit_tuple.append(4)
>>> small_numbers = tuple(edit_tuple)
>>> small_numbers
(0, 1, 2, 3, 4)
>>> type(small_numbers)
<class 'tuple'>
It should be noted that Lists can have its items modified and can also have items added to it. Sets can have items added to it but cannot have its items modified or changed.
4. Sets do not allow duplicate items, Lists and Tuples allow duplicate items. If a value in a set was written two or more times and printed out, the output would have only one of those duplicate values.
>>> colorList = ['red', 'orange', 'yellow', 'orange', 'red']
>>> colorTuple = ('red', 'orange', 'yellow', 'orange', 'red')
>>> colorSet = {'red', 'orange', 'yellow', 'orange', 'red'}
>>> colorList
['red', 'orange', 'yellow', 'orange', 'red']
>>> colorTuple
('red', 'orange', 'yellow', 'orange', 'red')
>>> colorSet
{'red', 'orange', 'yellow'}
5. Sets cannot hold mutable objects. for example, if you try to input a list in a set, you would get an error telling you that you are trying to put an unhashable type in the set. (unhashable means mutable basically).
>>> numbers = {1, 2, [3, 4, 5,], 6}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Similarities
There are no stand out similarities between Lists, Tuples and Sets. The similarities between any two of the three has been mentioned while stating the differences. For clarity purposes I will list them below:
- Lists and Sets are immutable data structures. They can be modified.
- Lists and Tuples are ordered. They can be accessed by indexing
- Lists and Tuples can hold duplicate values.
- Lists and Tuples can hold mutable objects like lists.
Conclusion
These different data structures have different situations where they can be used to optimize your code. For example, if you wanted to write coordinates of particular places, a tuple would be better since no two places can have the same coordinates.
Thanks for reading:).