The Noobs of Python: Ep.2.2 - A Look at Dictionaries [Updated]

Dictionaries !

Along with lists, dictionaries are one of the most flexible built-in data types in Python.
If you think of lists as ordered collections of objects, you can think of dictionaries as
unordered collections; the big distinction is that in dictionaries, items are stored and
fetched by key, instead of by position. Dictionaries take the place of records, search tables, and any other sort of aggregation where item names are more meaningful than item positions.

>>> avengers = {'Scarlet Witch': 'Wanda Maximoff', 'Thor': 'Eric Masterson',
'Ant-Man': 'Hank Pym'}

as you can see a dictionary is written as a series of key:value pairs, separated by commas, enclosed in curly braces.

Here’s a rundown of their main properties. Python dictionaries are:

  1. Accessed by key, not offset position
  2. Unordered collections
  3. Variable-length, heterogeneous, and arbitrarily nestable

Q: Those are some big words, Can you break that down a bit?
A: Here’s a breakdown.
Accessed by key, not offset positions
Dictionaries associate a set of values with keys, so you can fetch an item out of a dictionary using the key under which you originally storedit. You use the same indexing operation to get components in a dictionary as you do in a list, but the index takes the form of a key, not a relative offset.
The Noobs of Python : Ep.2 - List and List of List ! and the List goes on!

Unordered collections of arbitrary objects
Unlike in a list, items stored in a dictionary aren’t kept in any particular order; in
fact, Python pseudo-randomizes their left-to-right order to provide quick lookup.
Keys provide the symbolic (not physical) locations of items in a dictionary.

Variable-length, heterogeneous, and arbitrarily nestable
Like lists, dictionaries can grow and shrink in place, they can contain objects of any type,
and they support nesting to any depth (they can contain lists, other dictionaries, and so on).
Each key can have just one associated value, but that value can be a collection of multiple objects if needed, and a given value can be stored under any number of keys.

Q: Fair enough, can we see some in action ?
A: Here is a list of known Avengers and their alias.

avengers = {'Thor': 'Eric Masterson', 'Captain America': 'Steve Rodgers', 
'Hawkeye': 'Clint Barton', 'Power Man': 'Luke Cage', 
'Scarlet Witch': 'Wanda Maximoff', 'Quicksilver': 'Pietro Maximoff', 
'The Black Knight': 'Dane Whitmann', 'Black Panther': 'TChalla', 
'She-Hulk': 'Jennifer Walters', 'Wonder Man': 'Simon Williams', 
'Wasp': 'Janet Van Dyne', 'The Sentry': 'Robert Reynolds', 
'Iron Man': 'Tony Stark', 'Black Widow': 'Natasha Romanov', 
'Goliath': 'Dr.Bill Foster', 'Ant-Man': 'Hank Pym'}

This assigns a dictionary to the avengers variable. This dictionary’s keys are
'Thor' , 'Captain America' , 'Hawkeye' and so on. The values for these keys are 'Eric Masterson' , 'Steve Rodgers' ,and 'Clint Barton' , respectively. You can access these values through their keys:

>>> avengers['Thor']
'Eric Masterson'
>>> avengers['Black Panther']
'TChalla'
>>> avengers['Goliath']
'Dr.Bill Foster'
>>> 
>>> 'My favorite Thor host ' + avengers['Thor'] + ' was the best !!!'
'My favorite Thor host Eric Masterson was the best !!!'
>>>

Dictionaries can still use integer values as keys, just like lists use integers for indexes,
but they do not have to start at 0 and can be any number. ex:

>>> mighty_avengers = {1 : 'Captain Marvel', 2 : 'Ant Man', 3 : 'Falcon'}
>>> mighty_avengers
{1: 'Captain Marvel', 2: 'Ant Man', 3: 'Falcon'}
>>>

The built-in len function works on dictionaries too; it returns the number of items
stored in the dictionary or the length of its keys list. The dictionary in
operator allows you to test for key existence, and the keys method returns
all the keys in the dictionary. The latter of these can be useful for processing dictionaries
sequentially, but you shouldn’t depend on the order of the keys list.

>>> len(avengers)
16
>>> len(mighty_avengers)
3
>>> 'Wasp' in avengers
True
 >>> list(avengers.keys())
['Hawkeye', 'Captain America', 'Wonder Man', 'Scarlet Witch', 'Quicksilver',
'The Black Knight', 'Black Widow', 'Ant-Man', 'She-Hulk', 'Black Panther',
'The Sentry', 'Iron Man', 'Power Man', 'Thor', 'Goliath', 'Wasp']
>>>
>>>
>>> list(avengers.items())
[('Hawkeye', 'Clint Barton'), ('Captain America', 'Steve Rodgers'),
('Wonder Man', 'Simon Williams'), ('Scarlet Witch', 'Wanda Maximoff'),
('Quicksilver', 'Pietro Maximoff'), ('The Black Knight', 'Dane Whitmann'),
('Black Widow', 'Natasha Romanov'), ('Ant-Man', 'Hank Pym'), 
('She-Hulk', 'Jennifer Walters'), ('Black Panther', 'TChalla'), 
('The Sentry', 'Robert Reynolds'), ('Iron Man', 'Tony Stark'), 
('Power Man', 'Luke Cage'), ('Thor', 'Eric Masterson'),  
('Goliath', 'Dr.Bill Foster'), ('Wasp', 'Janet Van Dyne')]

Notice
The value returned by the value() and items() method are tuples of the key and value pair.
More to come in the next Ep.2.x Tuples

Here, the in and not in operators can check whether a value exists in a list. You can also use these operators to see whether
a certain key or value exists in a dictionary.

>>> 'Captain America' in avengers
True
>>> 'Captain America' in avengers.values()
False
>>> 'Captain America' in avengers.keys()
True
>>> 'Captain America' not in avengers
False
>>> 'Captain America' not in avengers.values()
True
>>>

Q: What else can I do with dictionaries? Can I add/ manipulate them?
A: YUP ! Dictionaries, like lists, are mutable, so you can change, expand, and shrink them in place
without making new dictionaries, simply assign a value to a key to change or create an entry.
The del statement works here, too! Here are some examples :

>>> mighty_avengers
{1: 'Captain Marvel', 2: 'Ant Man', 3: 'Falcon'}
>>> mighty_avengers[3] = ['Falcon', 'Sam Wilson', 'All-New Captain America']
>>> mighty_avengers
{1: 'Captain Marvel', 2: 'Ant Man', 
3: ['Falcon', 'Sam Wilson', 'All-New Captain America']}
>>> del mighty_avengers[1]
>>> mighty_avengers
{2: 'Ant Man', 
3: ['Falcon', 'Sam Wilson', 'All-New Captain America']}
>>> mighty_avengers[1] = 'Iron Man'
>>> mighty_avengers
{1: 'Iron Man', 2: 'Ant Man', 
3: ['Falcon', 'Sam Wilson', 'All-New Captain America']}
>>>

Q: This should keep me busy for some time, Thanks !
A: There is more to come. There's a lot to do with Dictionaries.
An entry to Methods to the Madness for dictionaries is on the way !

Code on Code_Warriors !

3 Likes

Here we go in depth on Dictionary methods : .get()

We'll start by opening up idle3 and creating 2 dictionaries : avengers and mighty_avengers

>>> 
>>> avengers = {'Thor': 'Eric Masterson', 
'Captain America': 'Steve Rodgers', 'Hawkeye': 'Clint Barton', 
'Power Man': 'Luke Cage', 'Scarlet Witch': 'Wanda Maximoff', 
'Quicksilver': 'Pietro Maximoff', 'The Black Knight': 'Dane Whitmann', 
'Black Panther': 'TChalla', 'She-Hulk': 'Jennifer Walters', 
'Wonder Man': 'Simon Williams', 'Wasp': 'Janet Van Dyne', 
'The Sentry': 'Robert Reynolds', 'Iron Man': 'Tony Stark', 
'Black Widow': 'Natasha Romanov', 'Goliath': 'Dr.Bill Foster', 
'Ant-Man': 'Hank Pym'}

>>> mighty_avengers = {1: 'Iron Man', 2: 'Ant Man', 
3: ['Falcon', 'Sam Wilson', 'All-New Captain America']}

Both of these are carried over from the previous post above, so if you haven't restarted your session you are good to go! If not just copy and paste these 2.

Here is a Method called get(). In realistic programs that gather data as they run, you often won’t be able to predict what will be in a dictionary before the program is launched, much less when it’s coded. Fetching a nonexistent key is normally an error, but the get() method returns a default value— None , or a passed-in default—if the key doesn’t exist. It’s an easy way to fill in a default for a key that isn’t present, and avoid a missing-key error when your program can’t anticipate contents ahead of time. Here are some examples:

>>> avengers.get('Thor')
'Eric Masterson'
>>> print(avengers.get('Spider-Man'))
None
>>> print(avengers.get('Storm-Trooper', 'Nothing to see here'))
Nothing to see here
>>>
1 Like

A possible application of the .get method could be looking across an array and applying a kernel operation across an image, so that when you apply the operation to the pixels around the edges of the image you don't get loads of errors for asking for pixels with negative x and y values.

Like getting the next iteration in the game of life! Sortakinda! Here's how I got the amount of a neighbours a given cell had assuming it was populated itself (hence the -1). Boolean True values in Python are equivalent to a 1 (True + True = 2).

sum([sum([self.table.get((xi, yi), False) for xi in range(x-1, x+2)]) for yi in range(y-1, y+2)])-1
2 Likes

It has been some time since we last covered Dictionaries, so here we go with the update method !

fire up Idle3 and lets reuse the previous dictionary avengers .

The update method provides something similar to concatenation for dictionaries, though it has nothing to do with left-to-right ordering (in case you forgot, there is no such thing in dictionaries). It merges the keys and values of one dictionary into another, blindly overwriting values of the same key if there’s a clash here's an example of merging 1 dictionary with another:

>>> avengers
{'Wonder Man': 'Simon Williams', 'Captain America': 'Steve Rodgers', 
'Black Panther': 'TChalla', 'Ant-Man': 'Hank Pym', 
'Scarlet Witch': 'Wanda Maximoff', 'She-Hulk': 'Jennifer Walters', 
'Quicksilver': 'Pietro Maximoff', 'Black Widow': 'Natasha Romanov', 
'Hawkeye': 'Clint Barton', 'Goliath': 'Dr.Bill Foster', 'Power Man': 'Luke Cage', 
'Thor': 'Eric Masterson', 'Iron Man': 'Tony Stark', 'The Black Knight': 'Dane Whitmann', 
'Wasp': 'Janet Van Dyne', 'The Sentry': 'Robert Reynolds'}
>>> avengers2 = {'Ms.Marvel': 'Carol Danvers'}
>>> avengers.update(avengers2)
>>> avengers
{'Ms.Marvel': 'Carol Danvers', 'Wonder Man': 'Simon Williams', 
'Captain America': 'Steve Rodgers', 'Black Panther': 'TChalla', 
'Ant-Man': 'Hank Pym', 'Scarlet Witch': 'Wanda Maximoff', 
'She-Hulk': 'Jennifer Walters', 'Quicksilver': 'Pietro Maximoff', 
'Black Widow': 'Natasha Romanov', 'Hawkeye': 'Clint Barton', 
'Goliath': 'Dr.Bill Foster', 'Power Man': 'Luke Cage', 'Thor': 'Eric Masterson', 
'Iron Man': 'Tony Stark', 'The Black Knight': 'Dane Whitmann', 
'Wasp': 'Janet Van Dyne', 'The Sentry': 'Robert Reynolds'}

Here is an example of the update method overwriting values of the same key, remember update merges the key value pair to the dictionary so it will overwrite asking no questions.

>>> avengers3 = {'Ms.Marvel': 'Kamala Khan', 'Goliath': 'Deceased', 
'The Sentry': 'Deceased', 'Thor': 'Jane Foster', 'Iron Man': 'Riri Wiliams'}
>>> avengers.update(avengers3)
>>> avengers
{'Ms.Marvel': 'Kamala Khan', 'Wonder Man': 'Simon Williams', 
'Captain America': 'Steve Rodgers', 'Black Panther': 'TChalla', 
'Ant-Man': 'Hank Pym', 'Scarlet Witch': 'Wanda Maximoff', 
'She-Hulk': 'Jennifer Walters', 'Quicksilver': 'Pietro Maximoff', 
'Black Widow': 'Natasha Romanov', 'Hawkeye': 'Clint Barton', 
'Goliath': 'Deceased', 'Power Man': 'Luke Cage', 'Thor': 'Jane Foster', 
'Iron Man': 'Riri Wiliams', 'The Black Knight': 'Dane Whitmann', 
'Wasp': 'Janet Van Dyne', 'The Sentry': 'Deceased'}
>>>

Another method that's pretty useful is the pop method also used in list but a little differently. The dictionary pop method deletes a key from a dictionary and returns the value it had. It’s similar to the list.pop() method, but it takes a key instead of an optional position here's an example:

>>> avengers.pop('The Sentry')
'Deceased'
>>> avengers.pop('Goliath')
'Deceased'
>>> avengers
{'Ms.Marvel': 'Kamala Khan', 'Wonder Man': 'Simon Williams', 
'Captain America': 'Steve Rodgers', 'Black Panther': 'TChalla', 
'Ant-Man': 'Hank Pym', 'Scarlet Witch': 'Wanda Maximoff', 
'She-Hulk': 'Jennifer Walters', 'Quicksilver': 'Pietro Maximoff', 
'Black Widow': 'Natasha Romanov', 'Hawkeye': 'Clint Barton', 
'Power Man': 'Luke Cage', 'Thor': 'Jane Foster', 'Iron Man': 'Riri Wiliams', 
'The Black Knight': 'Dane Whitmann', 'Wasp': 'Janet Van Dyne'}
>>>
1 Like