Python Programming with Corey Schafer
Lecture 4 – Lists, Tuples and Sets
• List and tuples allow us to work with sequential data
• Sets – unordered collection of values with no duplicates
• Making a list
courses = ['Maths, 'History', 'Physics']
print(courses)
→ ['Maths, 'History', 'Physics']
• Number of values in list
courses = ['Maths', 'History', 'Physics']
print(len(courses))
→3
It is because there are three values
• Accesing values in list by index – positive numbers
courses = ['Maths', 'History', 'Physics']
print(courses[0])
→ Maths
It is because index starts at 0 (1, 2 and so on…)
• Accesing values in list by index – negative numbers
courses = ['Maths', 'History', 'Physics']
print(courses[-1])
→ Physics
It is because negative index starts at -1 (-2, -3 and so on…). It is useful for getting the last item if
the list grew as -1 would always be the last item.
• List index out of range error for an index that doesn’t exist
• Accesing range of values in list by index
courses = ['Maths', 'History', 'Physics']
print(courses[0:2])
→ ['Maths', 'History']
First index is inclusive i.e. 0 while the last index is not i.e. 2
courses = ['Maths', 'History', 'Physics']
print(courses[:2])
→ ['Maths', 'History']
If first index is not mentioned it automatically means 0
courses = ['Maths', 'History', 'Physics']
print(courses[1:])
→ ['History', 'Physics']
If last index is not mentioned it automatically means the last value
• Add an item to the list (end of list)
courses = ['Maths', 'History', 'Physics']
courses.append('Arts')
print(courses)
→ ['Maths', 'History', 'Physics', 'Arts']
• Add an item to the list (at a specific location)
courses = ['Maths', 'History', 'Physics']
courses.insert(0, 'Arts')
print(courses)
→ ['Arts', 'Maths', 'History', 'Physics']
First argument is the location of the index and the second is the value
• Add multiple items to the list
courses = ['Maths', 'History', 'Physics']
courses_2 = ['Arts', 'Education']
courses.insert(0, courses_2)
print(courses)
→ [['Arts', 'Education'], 'Maths', 'History', 'Physics']
Creates a list within a list
courses = ['Maths', 'History', 'Physics']
courses_2 = ['Arts', 'Education']
courses.insert(0, courses_2)
print(courses[0])
→ ['Arts', 'Education']
The first value is this list itself
Solution is to use extend function
courses = ['Maths', 'History', 'Physics']
courses_2 = ['Arts', 'Education']
courses.extend(courses_2)
print(courses)
→ ['Maths', 'History', 'Physics', 'Arts', 'Education']
• Remove an item from the list
courses = ['Maths', 'History', 'Physics']
courses.remove('Maths')
print(courses)
→ ['History', 'Physics']
Alternatively
courses = ['Maths', 'History', 'Physics']
courses.pop()
print(courses)
→ ['Maths', 'History']
Last item was removed from the list
courses = ['Maths', 'History', 'Physics']
popped = courses.pop()
print(popped)
print(courses)
→ Physics
['Maths', 'History']
pop could be used to return the removed value
You could keep popping values until the list is empty
• Reverse the list
courses = ['Maths', 'History', 'Physics']
courses.reverse()
print(courses)
→ ['Physics', 'History', 'Maths']
• Sort the list alphabetically
courses = ['Maths', 'History', 'Physics']
courses.sort()
print(courses)
→ ['History', 'Maths', 'Physics']
• Sort the list in ascending order
nums = [1, 5, 2, 4]
nums.sort()
print(nums)
→ [1, 2, 4, 5]
• Sort the list in descending order
You could sort it in ascending order and then reverse it
Alternatively
courses = ['Maths', 'History', 'Physics']
nums = [1, 5, 2, 4]
courses.sort(reverse=True)
nums.sort(reverse=True)
print(courses)
print(nums)
→ ['Physics', 'Maths', 'History']
[5, 4, 2, 1]
• Sort the list while keeping the original unchanged
courses = ['Maths', 'History', 'Physics']
sorted_courses = sorted(courses)
print(courses)
print(sorted_courses)
→ ['Maths', 'History', 'Physics']
['History', 'Maths', 'Physics']
• Other functions
nums = [1, 5, 2, 4]
print(min(nums))
→1
It gives the minimum number
nums = [1, 5, 2, 4]
print(max(nums))
→5
It gives the maximum number
nums = [1, 5, 2, 4]
print(sum(nums))
→ 12
It gives the sum of all the numbers
• Find the index of a value
courses = ['Maths', 'History', 'Physics']
print(courses.index('Maths'))
→0
It shows that it is located at the first index. Error if the value does not exists
• Find if value exists in the list
courses = ['Maths', 'History', 'Physics']
print('Arts' in courses)
→ False
It shows that it does not exists in the list
courses = ['Maths', 'History', 'Physics']
print('Maths' in courses)
→ Ture
It shows that it exists in the list
• Loop through values with for loop
courses = ['Maths', 'History', 'Physics']
for item in courses:
print(item)
→ Maths
History
Physics
Values are printed in new line by default
• Access the index and value
courses = ['Maths', 'History', 'Physics']
for index, item in enumerate(courses):
print(index, item)
→ 0 Maths
1 History
2 Physics
courses = ['Maths', 'History', 'Physics']
for index, item in enumerate(courses, start=1):
print(index, item)
→ 1 Maths
2 History
3 Physics
Now the starting value would be 1
• Convert list to string
courses = ['Maths', 'History', 'Physics']
courses_str = ', '.join(courses)
print(courses_str)
→ Maths, History, Physics
courses = ['Maths', 'History', 'Physics']
courses_str = ' - '.join(courses)
print(courses_str)
→ Maths - History - Physics
• Convert string to list
courses = ['Maths', 'History', 'Physics']
courses_str = ' - '.join(courses)
new_list = courses_str.split(' - ')
print(courses_str)
print(new_list)
→ Maths - History - Physics
['Maths', 'History', 'Physics']
Splits values in the basis of ' - '
• Tuples are very similar to lists but cannot be modified. Lists are mutable while tuples are
immutable. Lists use [] while tuples use ()
• Modification of list
list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1
print(list_1)
print(list_2)
list_1[0] = 'Art'
print(list_1)
print(list_2)
→ ['History', 'Math', 'Physics', 'CompSci']
['History', 'Math', 'Physics', 'CompSci']
['Art', 'Math', 'Physics', 'CompSci']
['Art', 'Math', 'Physics', 'CompSci']
Changes both lists are they are modifiable
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1
print(tuple_1)
print(tuple_2)
tuple_1[0] = 'Art'
print(tuple_1)
print(tuple_2)
→ ('History', 'Math', 'Physics', 'CompSci')
('History', 'Math', 'Physics', 'CompSci')
error
Error because tuples do not support modification
• Sets use {}
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
print(cs_courses)
→ {'History', 'Math', 'Physics', 'CompSci'}
→ {'Math', 'CompSci', 'History', 'Physics'}
This will print different value each time as sets do not care about the order of values
• Sets are used to remove duplicate values
cs_courses = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print(cs_courses)
→ {'History', 'Math', 'Physics', 'CompSci'}
This will automatically remove the duplicate 'Math' and print it once
• Sets are used to test if either a value is part of a set
cs_courses = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print('Math' in cs_courses)
→ True
It is because 'Math' exists in the set
• Sets are used to determine values they share with other sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.intersection(art_courses))
→ {'History', 'Math'}
It gives back common values
• Sets are used to determine values they do not share with other sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.difference(art_courses))
→ {'CompSci', 'Physics'}
It gives back values of cs_courses that are not present in art_courses
• Combine values of sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.union(art_courses))
→ {'Art', 'CompSci', 'History', 'Design', 'Math', 'Physics'}
It gives back values of all values of both sets
• Creating empty lists, tuples and sets
empty_list = []
empty_list = list()
Both methods can be used
empty_tuple = ()
empty_tuple = tuple()
Both methods can be used
empty_set = {}
empty_set = set()
Only the second method can be used. The first method would create empty dictionary