Updating multiple items in a list using slice assignment
The syntax of the slice assignment is as follows:
list_object[start:stop:step] = iterable
For now, think of iterable as a sequence/container such as a list, string, tuple etc.
1days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]2print(days[1:3])3# ['Tue', 'Wed']45days[1:3] = [1, 2]6print(days)7# ['Mon', 1, 2, 'Thu', 'Fri', 'Sat', 'Sun']
1days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]2print(days[1::2])3# ['Tue', 'Thu', 'Sat']45days[1::2] = ["", "", ""]6print(days)7# ['Mon', '', 'Wed', '', 'Fri', '', 'Sun']89days[:3] = "MTW"10print(days)11# ['M', 'T', 'W', '', 'Fri', '', 'Sun']
Other list methods
1# list.reverse() : Reverse the list "in place"2grades = [90, 70, 60.5, 70, 80]3grades.reverse()4print(grades) # [80, 70, 60.5, 70, 90]56# list.clear() : Remove all items from list.7grades = [90, 70, 60.5, 70, 80]8grades.clear()9print(grades) # []
What modifies a list?
- Assigning a value to an element using its index.
1a = [1, 2, 3]2a[0] = 5
- Using the slice assignment to modify the elements of a list.
1a = [1, 2, 3]2a[:2] = [4, 5]
- Using methods like
append(), insert(), remove() , pop(), clear()
, etc.
What does not modify a list?
- Slicing! It is a useful tool to create new lists out of an existing lists.
1a = [1, 2, 3]2b = a[:] # makes a copy of a3print(a is b) # False
- The + and * operators create a new list
1a = [1, 2, 3]2b = a # does not copy, just a new name for same list3b = b + [4] # b now refers to a new list [1, 2, 3, 4]4 # a still refers to [1, 2, 3]5print(a is b) # False
Set methods
1# set.update(iterable):2# Adds all items from the iterable to the set.34numbers = {1, 2, 3}5numbers.update([10, 2, 2, 3, 20])6print(numbers) # {1, 2, 3, 10, 20}78numbers.update(("a", "b"))9print(numbers) # {1, 2, 3, 'a', 10, 20, 'b'}1011primes = {2, 3, 5}12primes.update({5, 7, 11})13print(primes) # {2, 3, 5, 7, 11}
1# set.clear(): Remove all elements from this set.23numbers = {1, 2, 3}4numbers.clear()5print(numbers) # set()
1# For the following methods, suppose A and B are sets.23# A.intersection(B):4# Returns a new set that contains elements that are5# present in both A and B67odd = {3, 5, 7, 9, 25}8squares = {4, 9, 25, 36}9odd_squares = odd.intersection(squares)10print(odd_squares) # {9, 25}1112# Intersection can also be done using operator &13odd_squares = odd & squares14print(odd_squares) # {9, 25}
1# A.union(B):2# Returns a new set that contains elements that are3# present in A or B or both45x = {1, 2, 3}6y = {2, 3, 5}7all_numbers = x.union(y)8print(all_numbers) # {1, 2, 3, 5}910# Same above but using an operator |11all_numbers = x | y12print(all_numbers) # {1, 2, 3, 5}
1# A.difference(B):2# Returns a new set that contains elements that are3# present only in A but not in B4x = {1, 2, 3}5y = {2, 3, 5}6diff = x.difference(y)7print(diff) # {1}89# Same as above but using operator -10diff = x - y11print(diff) # {1}
All of the set methods work the same when elements are of other types such as strings.
Dictionaries
Suppose we would like to store the following enrollment data:
semester | no. of students |
---|---|
F2017 | 816 |
W2018 | 613 |
F2018 | 709 |
W2019 | 590 |
We can do this using two lists for the two columns:
1semesters = ['F2017', 'W2018', 'F2018', 'W2019']2students = [816, 613, 709, 590]
What should we do if we want to add new data?
1semesters = ['F2017', 'W2018', 'F2018', 'W2019']2students = [816, 613, 709, 590]34semesters.append("F2020")5students.append(550)6# ['F2017', 'W2018', 'F2018', 'W2019', 'F2020']7# [816, 613, 709, 590, 550]
What if we want to modify the value for a specific semester?
1semesters = ['F2017', 'W2018', 'F2018', 'W2019']2students = [816, 613, 709, 590]34idx = semesters.index("W2018")5students[idx] = 600
What we if try to add an entry for a semester that already exists?
List allows duplicates so it does not check if a semester already exists.
1semesters = ['F2017', 'W2018', 'F2018', 'W2019']2students = [816, 613, 709, 590]34semesters.append("F2018")5students.append(500)6# ['F2017', 'W2018', 'F2018', 'W2019', 'F2018']7# [816, 613, 709, 590, 500]
Use a dictionary!
- You can think of an item of a dictionary as a pair of objects:
- The first object of the pair is called a key.
- The second object is referred to as the value.
- A dictionary is called a mapping type because it maps key objects to value objects.
1# A dictionary is created using a sequence of key-value pairs2enrollment = {'F2017': 816, 'W2018': 613,3 'F2018': 709, 'W2019': 590}45print(type(enrollment)) # <class 'dict'>
1# Number of key-value pairs2print(len(enrollment)) # 434# This is an empty dictionary, not a set!5empty_dict = {}6print(len(empty_dict)) # 0
Dictionary Examples
1# Key: a number, Value: True if number is prime, else False2is_prime = {2: True, 3: True, 4: False, 5: True,3 7: True, 10: False}45# Key: inventory items, Value: count of items in inventory6inventory = {"sofa": 5, "table": 10, "chair": 20, "mattress": 5}78# Key: city name, Value: area of city9population = {"Montreal": 431.50, "Toronto": 630.20}1011# Key: country name, Value: capital city12capitals = {"Canada": "Ottawa", "United States": "Washington, D.C.",13 "France": "Paris", "Germany": "Berlin"}
Note on keys and values
- Keys
- Have to be immutable objects.
- Have to be unique in a dictionary. A dictionary cannot contain two items with the same key.
- Values
- Values can be of any type; both mutable and immutable values are allowed.
- Many keys can map to the same value. i.e. values need not be unique.
Dictionary Lookup
With lists, we can access an item of the list through its index.
With dictionaries, we can access a value stored in the dictionary through the key associated with it.
1enrollment = {'F2017': 816, 'W2018': 613, 'F2018': 709,2 'W2019': 590, 'F2019': 744}34num_students = enrollment["F2018"]5print(num_students) # 70967# Key must exist in the dictionary if we want to access its value8print(enrollment["F2020"]) # KeyError: 'F2020'
Adding an item
We can add a new item by specifying a key and a value: dictionary[key] = value
1enrollment = {'F2018': 709, 'W2019': 590}23enrollment["F2020"] = 800 # add an item4enrollment["W2020"] = 900 # add another item56# {'F2018': 709, 'W2019': 590, 'F2020': 800, 'W2020': 900}78enrollment["F2018"] = 700 # change an existing item910# {'F2018': 700, 'W2019': 590, 'F2020': 800, 'W2020': 900}
Removing an item
We can delete an item using the following syntax: del dictionary[key]
1enrollment = {'F2018': 709, 'W2019': 590, 'F2019': 744}23del enrollment["F2019"]45# {'F2018': 709, 'W2019': 590}67del enrollment["F2020"]8# KeyError: 'F2020'
What will be printed in the following examples?
1d = {'x' : 0, 'y' : 1, 'z' : 2}2x = d['y']3print(x)
1d = {'x' : 0, 'y' : 1, 'z' : 2}2x = d[0]3print(x)
Check for membership
We can check if a key is part of a dictionary using the in and not in operators.
1d = {'x' : 0, 'y' : 1, 'z' : 2}2print('x' in d) # True3print(0 in d) # False4print(0 not in d) # True
Iterating through a dictionary
We can use a for loop to iterate through all the keys in a dictionary.
1enrollment = {'F2018': 709, 'W2019': 590, 'F2019': 744}23for key in enrollment:4 print(key, "->", enrollment[key])
F2018 -> 709 W2019 -> 590 F2019 -> 744
Functions and methods for dictionaries
1# dict(L): creates and returns a dictionary using a list L of tuples,2# where each tuple is of length 2 in form of (key, value).34pairs = [("Montreal", 1.78), ("Rome", 2.87), ("Tokyo", 9.27)]5population_data = dict(pairs)67print(population_data)8# {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}910print(population_data["Rome"])11# 2.87
1population_data = {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}23# dict.keys(): returns a iterable (sequence) of all keys45cities = list(population_data.keys())6print(cities) # ['Montreal', 'Rome', 'Tokyo']78# dict.values(): returns a iterable (sequence) of all values910population = list(population_data.values())11print(population) # [1.78, 2.87, 9.27]
1# dict.items(): returns a iterable (sequence) of tuples (key, value)2# for all items in the dictionary34population_data = {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}5pairs = list(population_data.items())67print(pairs)8# [('Montreal', 1.78), ('Rome', 2.87), ('Tokyo', 9.27)]
For more methods, use help(dict).
Using the dict methods in for loop:
1population_data = {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}23total = 04for population in population_data.values():5 total += population67print(total) # 13.92
1population_data = {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}23# dict.items() returns an iterable of key-value tuples45for tup in population_data.items():6 city = tup[0]7 population = tup[1]8 print(city, "->", population)910# Montreal -> 1.7811# Rome -> 2.8712# Tokyo -> 9.27
Making use of tuple unpacking in the for loop:
1population_data = {'Montreal': 1.78, 'Rome': 2.87, 'Tokyo': 9.27}23for city, population in population_data.items():4 print(city, "->", population)56# Montreal -> 1.787# Rome -> 2.878# Tokyo -> 9.27
Time for some problems on Ed Lessons.