Docsity

Python Dictionaries: A Comprehensive Guide, Schemes and Mind Maps of Advanced Computer Programming

Partial preview of the text

Download Python Dictionaries: A Comprehensive Guide and more Schemes and Mind Maps Advanced Computer Programming in PDF only on Docsity!

Chapter 6 Dictionaries

April 9, 2020

A Simple Dictionary

Working with Dictionaries

Looping Through a Dictionary

Nesting

Accessing Values in a Dictionary

▶ (^) To get the value associated with a key, give the name of the dictionary and then place the key inside a set of square brackets ▶ (^) alien_0 = {’color’: ’green’} ▶ (^) print(alien_0[’color’]) ▶ (^) green

Adding New Key-Value Pairs

▶ (^) Dictionaries are dynamic structures, and you can add new key-value pairs to a dictionary at any time. ▶ (^) alien_0 = {’color’: ’green’, ’points’: 5} ▶ (^) print(alien_0) ▶ (^) alien_0[’x_position’] = 0 ▶ (^) alien_0[’y_position’] = 25

Modifying Values in a Dictionary

▶ (^) To modify a value in a dictionary, give the name of the dictionary with the key in square brackets and then the new value you want associated with that key. ▶ (^) alien_0 = ’color’: ’green’ ▶ (^) alien_0[’color’] = ’yellow’

Removing Key-Value Pairs

▶ (^) use the del statement to completely remove a key -value pair. ▶ (^) All del needs is the name of the dictionary and the key that you want to remove. ▶ (^) alien_0 = {’color’: ’green’, ’points’: 5} ▶ (^) del alien_0[’points’] ▶ (^) Be aware that the deleted key-value pair is removed permanently.

Using get() to Access Values

▶ (^) Using keys in square brackets to retrieve the value you’re interested in from a dictionary might cause one potential problem: if the key you ask for doesn’t exist, you’ll get an error. ▶ (^) For dictionaries, specifically, you can use the get() method to set a default value that will be returned if the requested key doesn’t exist. ▶ (^) point_value = alien_0.get(’points’, ’No point value assigned.’) ▶ (^) print(point_value) ▶ (^) No point value assigned.

Looping Through All Key-Value Pairs

▶ (^) A single Python dictionary can contain just a few key -value pairs or millions of pairs. ▶ (^) You can loop through all of a dictionary s key -value pairs, through its keys, or through its values. ▶ (^) user_0 = {’username’: ’efermi’, ’first’: ’enrico’, ’last’: ’fermi’,} ▶ (^) for key, value in user_0.items(): ▶ (^) print(f”\nKey: key”) ▶ (^) print(f”\Value: value”)

Looping Through a Dictionary s Keys in a Particular

Order

▶ (^) Starting in Python 3.7, looping through a dictionary returns the items in the same order they were inserted ▶ (^) One way to do this is to sort the keys as they’re returned in the for loop. You can use the sorted() function to get a copy of the keys in order ▶ (^) for name in sorted(favorite_languages.keys()):

Looping Through All Values in a Dictionary

▶ (^) If you are primarily interested in the values that a dictionary contains, you can use the values() method to return a list of values without any keys. ▶ (^) for language in favorite_languages.values(): ▶ (^) To see each language chosen without repetition, we can use a set. ▶ (^) When you wrap set() around a list that contains duplicate items, Python identifies the unique items in the list and builds a set from those items. ▶ (^) for language in set(favorite_languages.values()):

A List in a Dictionary

▶ (^) It s sometimes useful to put a list inside a dictionary. ▶ (^) pizza = ’crust’: ’thick’, ’toppings’: [’mushrooms’, ’extra cheese’], ▶ (^) The list of toppings is a value associated with the key ’toppings’. ▶

A Dictionary in a Dictionary

▶ (^) You can nest a dictionary inside another dictionary, but your code can get complicated quickly when you do.

▶ (^) dictionary.items()

▶ (^) dictionary.keys()

▶ (^) dictionary.values()