A discussion of the differences between lists, sets, tuples, and dictionaries in Python.
Comparison Chart
List | Set | Tuple | Dictionary | |
---|---|---|---|---|
Ordered | x | x | ||
Mutable (value can change) | x | x | x | |
May contains duplicates | x | x | ||
Basic syntax | list1 = [] | set1 = {} | tuple1 = () | dict1 = {‘key’:’value’} |
Table of Contents:
Lists (ordered data set)
A list is an ordered data structure that stores more than one value in one variable space. Imagine owning a 3 disc DVD collection that comes in one case. Reaching for one case gives you access to each of the individual discs once you open it.
In Python we create a variable to store the list, then include each value within brackets:
dm_series = ['Despicable Me', 'Despicable Me 2', 'Despicable Me 3']
To access these individual values, we use the index value. Think of lists as having an invisible number below each value. We use that number to access the value. For example:
dm_series = ['Despicable Me', 'Despicable Me 2', 'Despicable Me 3'] 0 1 2
To access Despicable Me 2, call the list and include the index number in brackets:
dm_series[1]
Common ways to use lists:
- Create an empty list: my_cars = []
- Print a value: print(my_cars[1])
- Select a slice of the list: my_cars[0:1]
Sets (unordered data set)
Sets are collections of unique elements without any particular ordering. Ordering changes how we sort and modify collections. For example, using .pop() on a list removes the last item, but .pop() on a set removes a random element because there is no “last element”.
In Python, we create a variable to store the set, then include each value within parentheses.
my_cars = ('Mustang', 'Fusion', 'Escape')
Common ways to use sets:
- Create an empty set: my_cars = set()
- Conver a list to set: my_car_set = set([”Dodge Ram,’Ford Fusion’])
- Iterate over a set:
colors = set([‘Pthalo Blue’, ‘Indian Yellow’, ‘Sap Green’])
for color in colors:
print(color)
Tuples (ordered, immutable data set)
Tuples store related pieces of information and are useful when you have two or more values that are so closely related that they will always be used together, like latitude and longitude coordinates. A common use for tuples is to return multiple values from a function.
Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indexes.
Unlike lists and sets, tuples are immutable. You can’t add and remove items from tuples, or sort them in place.
Create a tuple in Python:
statue_liberty = (''40.689247, '-74.044502')
Dictionaries (unordered data set, stored with key)
Dictionaries store pairs of elements: keys and values. A database could store names and social security numbers as a dictionary because each social security number (key) is unique and is used to identify the name (value).
Create a dictionary with Python:
my_cars = {'Dodge': 1976, 'Ford': 1955, 'Toyota': 2018}
Common ways to use dictionaries:
- Print a value: print(my_cars[Toyota]), answer is 2018
- Iterate over a dictionary:
Beatles_Discography = {“Please Please Me”: 1963, “With the Beatles”: 1963,
“A Hard Day’s Night”: 1964, “Beatles for Sale”: 1964, “Twist and Shout”: 1964,
“Help”: 1965, “Rubber Soul”: 1965, “Revolver”: 1966,
“Sgt. Pepper’s Lonely Hearts Club Band”: 1967,
“Magical Mystery Tour”: 1967, “The Beatles”: 1968,
“Yellow Submarine”: 1969 ,’Abbey Road’: 1969,
“Let It Be”: 1970}
for album_title in Beatles_Discography:
print(“title: {}, year: {}”.format(album_title, Beatles_Discography[album_title]))