Python lists vs sets vs tuples: A showdown between 3 essential data types
Review these fundamental Python data structures that every dev should know
Hey Grokking Python readers!
Welcome to a fresh edition of the newsletter! We hope you loved our earlier fun and humorous publication, 9 ways learning Python is like getting a pet python! Today we will return to some basics of Python and learn more about essential data structures.
If you already consider yourself a Python-fluent individual or guru, you may feel you have all the important data structures right at your fingertips. And that's great! If you don’t, then don’t worry, this edition is here to help you learn or refresh your memory.
In Python, built-in data structures help us hold a collection of different elements. Today, we will explore three of these data structures: lists, sets, and tuples. We will share some code samples and also explore some similarities and differences among these data structures.
Python lists
In Python, a list is a built-in data structure containing a collection of different items such as integers, characters, variables, etc. Let's dive straight into an example to help us understand a Python list better.
We'll create a list of four seasons in the year. We'll also follow syntax rules by ensuring that we are enclosing our list elements in square brackets [ ]
and separating each element with a comma.
seasons_list = [ 'spring', 'summer', 'fall', 'winter']
List elements have two main characteristics: they are ordered, and they also allow duplicates. This means that we can repeat an element twice if we want to.
We can also perform fancier functions like iterating, accessing, modifying, and removing elements from lists. Let's learn by example:
Iterating over list elements
Here’s what the code would look like:
for season in seasons_list:
print(season)
Output:
spring
summer
fall
winter
Modifying list elements
Because lists are mutable, we can modify elements within them. This implies we can add, remove, and change elements. For instance, append()
will allow us to add a new element to the end of the list.
seasons_list.append('snow')
print(seasons_list)
Output:spring
summer
fall
winter
snow
Accessing elements
We can use an index position to access an element from a list. By default, all index positions start at 0
, not 1
. For example, to access summer
from the list, we will need to query using 1
to get the desired output:
print(seasons_list_list[1])
Output:summer
Sets
Sets, like lists, are built-in data structures in Python that hold various elements. Like their list counterparts, we can perform various functions on sets, too. But the key distinguishing feature of sets is that they are unordered, do not allow duplicate values, and are enclosed in curly braces {}
.
Let's run through a few examples:
Iterating over sets
Here’s what the code would look like:
seasons_set = { 'spring', 'summer', 'fall', 'winter'}
for season in seasons_set:
print(season)
Output: spring
winter
fall
summer
Modifying sets
Sets are mutable, implying we can add or remove elements. To add a single element, we can use the add()
method, and to remove an element from a set, a remove()
method.
Adding an element
seasons_set = { 'spring', 'summer', 'fall', 'winter'}
seasons_set.add('foggy weather')
print(seasons_set)
Output:{'fall', 'summer', 'foggy weather', 'spring', 'winter'}
Removing an element
seasons_set = { 'spring', 'summer', 'fall', 'winter','foggy weather'}
seasons_set.remove('foggy weather')
print(seasons_set)
Output:{'winter', 'spring', 'summer', 'fall'}
Of course, you can do a lot more to work with sets, including popping items and more.
Tuples
A tuple is also a built-in data structure in Python similar to lists and sets but different in some ways. The main difference between a tuple and the other data structures is that tuple elements are enclosed in parentheses ()
. Elements are also separated by a comma.
Let's look at some examples of tuple data structures in action:
seasons_tuple = ('spring', 'summer', 'fall', 'winter')
In the example above, we listed a tuple with elements of the year's seasons. It should be noted that tuples are ordered and also allow duplicates.
Iterating over tuples
Here’s what the code would look like:
seasons_tuple = ('spring', 'summer', 'fall', 'winter')
for i in seasons_tuple:
print(i)
Output: spring
summer
fall
winter
Accessing tuple elements
Like lists and sets, tuples too can be accessed via index positions. Let's take a look at accessing a tuple below:
seasons_tuple = ('spring', 'summer', 'fall', 'winter')
print(seasons_tuple[2])
Output: fall
Differences and similarities
Now that we have touched on the basics of all three data structures, let's now look at some key differences and similarities between Python lists, sets, and tuples.
Lists
Use square brackets []
Duplicates are allowed
Mutable
Ordered
Sets
Use curly braces { }
Duplicates are not allowed
Mutable
Unordered
Tuples
Use parentheses
Duplicates are allowed
Immutable
Ordered
More on Python data structures
That's not all, though. We have just touched the surface of what you can do with tuples, sets, and lists. Python does have other in-built data structures, too, like dictionaries. To learn more about Python data structures and related topics, we encourage you to check out the following courses!
Additionally, learning Python can be fun! With the course Learn Python 3 from Scratch, you can learn the language and do fun things like making games!
As always, happy learning!