Dictionaries (2/2)
Let's consider some useful methods for dictionaries:
len(d)
- returns length of dictionary d (number of pairs key:value);min(d), max(d)
- minimum or maximum of keys of dictionary d;d.copy()
- returns copy of dictionary d;d.items()
- returns all pairs (key, value) in dictionary d;d.keys()
- returns all keys of dictionary d;d.values()
- returns all values of dictionary d;d[k] = e
- set valuee
to keyk
.
For example, traditionally, let's update our dictionary so it will contain two more countries:
Country | Area | Population |
---|---|---|
Brazil | 8515767 | 212559417 |
India | 3166391 | 1380004385 |
123456# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
Swipe to start coding
Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:
Name | Age | Height |
---|---|---|
John | 41 | 185 |
Michelle | 35 | 165 |
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.33Awesome!
Completion rate improved to 2.33
Dictionaries (2/2)
Let's consider some useful methods for dictionaries:
len(d)
- returns length of dictionary d (number of pairs key:value);min(d), max(d)
- minimum or maximum of keys of dictionary d;d.copy()
- returns copy of dictionary d;d.items()
- returns all pairs (key, value) in dictionary d;d.keys()
- returns all keys of dictionary d;d.values()
- returns all values of dictionary d;d[k] = e
- set valuee
to keyk
.
For example, traditionally, let's update our dictionary so it will contain two more countries:
Country | Area | Population |
---|---|---|
Brazil | 8515767 | 212559417 |
India | 3166391 | 1380004385 |
123456# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
Swipe to start coding
Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:
Name | Age | Height |
---|---|---|
John | 41 | 185 |
Michelle | 35 | 165 |
Løsning
Tak for dine kommentarer!
single
Awesome!
Completion rate improved to 2.33
Dictionaries (2/2)
Stryg for at vise menuen
Let's consider some useful methods for dictionaries:
len(d)
- returns length of dictionary d (number of pairs key:value);min(d), max(d)
- minimum or maximum of keys of dictionary d;d.copy()
- returns copy of dictionary d;d.items()
- returns all pairs (key, value) in dictionary d;d.keys()
- returns all keys of dictionary d;d.values()
- returns all values of dictionary d;d[k] = e
- set valuee
to keyk
.
For example, traditionally, let's update our dictionary so it will contain two more countries:
Country | Area | Population |
---|---|---|
Brazil | 8515767 | 212559417 |
India | 3166391 | 1380004385 |
123456# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
Swipe to start coding
Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:
Name | Age | Height |
---|---|---|
John | 41 | 185 |
Michelle | 35 | 165 |
Løsning
Tak for dine kommentarer!