Python Basics - Lists, Sets and Tuples
How to create and use lists, sets, and tuples in Python
If you’re new to Python and have not yet reviewed the basics, I would encourage you to review these articles first:
In this section, we will discuss working with lists, tuples, and sets. These are iterable data types, which means we can store multiple elements and iterate through them to apply various operations. We covered the main concepts in the last post, but let’s review the basics first. Create 3 iterable objects::
list1 = [1,3,7,8] # we create lists using square brackets
tuple1 = (1,3,8,7) # tuples are created with round brackets
set1 = {1,3,4,5} # sets are created with curly bracketsEach of these data types is an example of a class. In Object Oriented Programming, we use classes to create data types which hold certain attributes and have specific methods (or functions) that can be only used in the class. Sometimes you may see classes which have similar (or even with the same name) functions, however these are defined separately in each class. We will discuss classes more in a separate module.
Iterables can hold any type of data, including integers, floats, and strings. They can also be heterogeneous, meaning they can hold a mix of data points, however we should always try to keep them homogeneous (i.e., all numbers or all strings). This is because when we program, it’s very inefficient to isolate specific elements within an iterable, and it’s much more efficient to conduct operations on a full object of homogeneous data.
Lists
Let’s look at a few basic operations with lists. We will start with slicing. Slicing refers to isolating one or more variables in a list, based on position in the list. Positions in Python objects begin at count 0. Thus, the first element is at position 0, the second is at position 1, the third is at position 2 and so on. Using a Jupyter notebook, try the following code:
list1 = [1,3,7,8,10,11,12]
# slicing lists
list1[0] # this will return 1, the element in the first position
list1[1] # this will return 3, the element in the second position
list1[-1] # this will return 11, the element in the last position
list1[-2] # this will return 10, the element in the second lasts position
list1[0:4] # this will return elements in positions 0, 1, 2 and 3; the range is always "up to, but not including"
list1[:4] # this notation will return the same as above; if starting from the beginning of the list, we don't need to specify the position 0 starting point
list1[4:] # this will return all elements starting at position 4 until the end of the list
list[4:6] # this notation will return elements in positions 4 and 5We can also use slicing for item assignment. This means that we can replace certain elements in a list object. The fact that we can replace elements, makes lists “mutable” (i.e., they support changes or item assignment). Not all objects are mutable - for example, tuples are not. Let’s try:
# define a list
list1 = [1,3,7,8,10,11,12]
# change the 3rd element
list1[2] = 13
# display the list to validate the change has happened
print(list1)In addition to slicing and item assignment, the list class offers several methods (i.e., functions) we can work with. Try the following:
# define a list
list1 = [1,3,7,8,10,11,12]
# we can use the append() method to add an item to a list
list1.append(3)
# we can use the expand() method to combine 2 lists together
list2 = [1,3,6]
list1.expand(list2)
# we can count certain elements in a list, for example, how many 8s?
list1.count(8)
# using slicing, we can find values based on a position. To find a value within a certain position, we can use the index() method. Let's find which position the value of 7 is in
list1.index(7)
# we can insert an element in a list in a specific position using
# the first parameter is the position, and the second parameter is the value; in this case, we are inserting the value 18 in position 3
list1.insert(3,18)
# we can use the sort() method to sort the list from smallest to largest
list1.sort()
# we can use the reverse() method to sort the list from largest to smallest
list1.reverse()As you can see, lists are quite a flexible class to work with. In the real world, we can use them to understand and process data. For example, suppose you had a dataset with customer information which included a column with customer names. Perhaps, you need to isolate and list just the names of specific customers. You could use this class to organize and manipulate that information.
Tuples
Tuples are characterized by round brackets. They don’t have as many methods as lists. They are also not mutable, so we can’t use item assignment, or add, insert, or expand statements. We can slice tuples in the same way as lists, and we can use the count() and index() methods in the same way as lists. Let’s try:
tuple1 = (1,2,3,4)
# return the 1st element of the tuple
tuple1[0]
# return the last element of the tuple
tuple1[-1]
# count the number of 3s in the tuple
tuple1.count(3)
# find the position of the value 4 within the tuple
tuple1.index(4)Sets
Sets are iterable objects which uses curly brackets to be defined. They only hold unique values; you can convert a list to a set, for example, and this would eliminate any duplicates in data. Sets, like tuples, also do not support item assignment, and they cannot be sliced. However, we can add or remove elements from a set. We do have the option to create a frozen set, which is an immutable version of a set.
Since they hold unique values only, sets are a great object to use when comparing or combining unique values from multiple datasets. For example, suppose you had two tables of customers and you wanted to find only unique customers who exist across the two tables; you could isolate names from the first table in a set, names from the second table in a set, and compare/contrast/combine the two sets to create one set of unique values. Let’s look at some basic set methods:
set1 = {1,7,8,9}
set2 = {7,8,2,4,5}
# add value to a set
set1.add(3)
print(set1)
# you can also use update() to add values to a set from another set
set1.update(set2)
# remove value from a set
set1.discard(9)
print(set1)
# you can also use remove() to remove a value from a set
set2.remove(2)
print(set2)
# use intersection() to find common elements between two sets
print(set1.intersection(set2))
# use intersection_update() to update the original set (set1) to only include common elements from both sets
print(set1.intersection_update(set2))
# use difference() to find the elements between two sets which don't match
set1 = {1,7,8,9}
set2 = {7,8,2,4,5}
print(set1.difference(set2))
# use difference_update() to update the original set (set1) to only include different elements from both sets
print(set1.difference_update(set2))
# to create an immutable set which cannot be changed with any of the above methods, use frozenset()
fSet = frozenset(list1)This covers the basic functionalities of working with iterable objects. In the next section, we will cover working with dictionaries which have key : value pairs, as well as conditional statements and loops.

