Dictionaries

Definition:  awesome, but kind of annoying.

What is a dictionary?

  • A collection of key/value pairs
  • Can contain any data type
  • Have no order
  • Can be of any size

Basic Syntax

{key: value}

Key/value pairs are grouped in {} and separated by a colon.

Unique keys

Keys in dictionaries must be unique

Keys can be any date type

But be careful with this.

Values can be any data type.

You can go nuts, but working with complex dictionaries will be harder.

Making a dictionary

>>> mydict = {}
>>> 
>>> mydict['key1'] = 'my first value'
>>> mydict['key2'] = 'my second value'
>>> 
>>> mydict
{'key2': 'my second value', 'key1': 'my first value'}

Remember that it needs to exist to be mutated.

use the

dict[key] = value

to add values to your dictionary

Accessing values

>>> mydict
{'key2': 'my second value', 'key1': 'my first value'}

>>> mydict['key1']
'my first value'
>>> mydict['key2']
'my second value'

Call a value from your dictionary with the

dict[key]

formula

Uhh, but how do I know what the keys are?

dict.keys()

Returns a list of all the keys.

Change a current value

>>> mydict
{'key2': 'my second value', 'key1': 'my first value'}
>>> mydict['key1']
'my first value'
>>> mydict['key2']
'my second value'
>>> mydict['key2'] = 'this is another value'
>>> mydict['key2']
'this is another value'

Use the same assignment syntax.

How do I loop through a dictionary?

There are many ways.

They all are super annoying.

>>> for something in mydict:
...     print something
... 
key2
key1

wat.

 

Where are the values?

 

Bookmark this:
https://docs.python.org/2/library/stdtypes.html#dict

>>> countedlist
{'blue': 11, 'green': 10, 'red': 9}
>>> for key, value in countedlist.iteritems():
...     print key, value
... 
blue 11
green 10
red 9
>>> players = {1:{'name':'foo'}, 
2:{'name': 'fizzy'}, 
3: {'name': 'pop'}}
>>> for key, value in players.iteritems():
...     innerdictkeys = value.keys()
...     print key,
...     for item in innerdictkeys:
...         print value[item]

But I think I hate dictionaries.

That's fine.

That's normal.

The only thing that makes it better is practice.

You will need to use them!

Dealing with data becomes easier with dictionaries.

But wow are they the worst

Seriously, practice.

A basic example

Counting things

>>> from collections import Counter
>>> import random

>>> letters = ['a','b','c']
>>> mylist = [random.choice(letters) for i in range(50)]

>>> mylist
['b', 'a', 'b', 'c', 'c', 'a', 'b', 'b', 'c', 'b', 
'b', 'a', 'c', 'c', 'c', 'a', 'a', 'b', 'a', 'b', 
'b', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 
'c', 'b', 'a', 'a', 'a', 'b', 'a', 'c', 'c', 'a', 
'b', 'a', 'c', 'a', 'c', 'b', 'a', 'b', 'c', 'c']

>>> countedlist = Counter(mylist)
>>> countedlist
Counter({'a': 17, 'b': 17, 'c': 16})

That's awesome. How do we do this by hand?

>>> colors = ['red','green','blue']
>>> mylist = [random.choice(colors) for i in range(30)]
>>> countedlist = {}
>>> mylist
['red', 'green', 'green', 'green', 'red', 'blue', 
'green', 'red', 'blue', 'blue', 'green', 'red', 
'red', 'blue', 'blue', 'blue', 'green', 'green', 
'blue', 'blue', 'blue', 'red', 'red', 'red', 
'green', 'green', 'blue', 'red', 'green', 'blue']

>>> for candy in mylist:
...     if candy in countedlist:
...         countedlist[candy] += 1
...     else:
...         countedlist[candy] = 1
... 
>>> countedlist
{'blue': 11, 'green': 10, 'red': 9}

Dictionaries

By Elizabeth W.

Dictionaries

  • 749