We’ve known lists. This time, we’re gonna say hi to another kind of lists: dictionaries. Yes, dictionaries are lists, but they do not manage their values by integer indices running from 0 to n. Their indices could be any type, and are called keys.

## Create a Dictionary

You can create an empty dictionary then add pairs of values into it.

>>> d = dict()
>>> d['KT'] = 1608
>>> d['SC'] = 16
>>> d
{'KT': 1608, 'SC': 16}


Or you can create a dictionary with existing values

>>> d = {'SC': 16, 'KT': 1608, 'An': 2608}


Three things to take away:

• Keys and values in dictionaries are enclosed in squiggly brackets
• Items on the right-hand side are the keys, the others are values. Or in case of adding separately, the one in the square brackets is the key.
• The order of those pair could not be the same

## Let’s Try Some Tricks

• index
Because dictionaries don’t use indices, you can’t call a value by its index, but by its key.
>>> d['KT']
1608

• changing
Dictionaries are mutable, so
>>> d['KT'] = 16
>>> d
{'SC': 16, 'KT': 16, 'An': 2608}

• values()
In case you want to look up a value but not sure about its key, you can use values() to return a collection of all values, then check if your value is in that collection or not.
>>> val = d.values()
>>> 16 in val
True

No, you cannot do that.

## Count in Dictionaries

A common use for dictionaries (or a common exercise) is to use dictionaries to count the number of times a word appearing in a file, or how many times each letter appears in a string.

The method is simple. You read through a file, word by word. Every time you meet a new word, you give it a number 1. If you’ve already gone through it, you add 1 to its count. Method for counting in strings works the same way. I’m gonna translate it into Python.

d = dict()
strg = 'Coding is fun'
for char in strg:
if char == ' ': continue
if char not in d:
d[char] = 1
else:
d[char] +=1
print(d)


Output

{'C': 1, 'o': 1, 'd': 1, 'i': 2, 'n': 2, 'g': 1, 's': 1, 'f': 1, 'u': 1}


Dictionary has a method called get that will shorten these steps for you.

d = dict()
strg = 'Coding is fun'
for char in strg:
if char == ' ': continue
d[char] = d.get(char,0) +1
print(d)


Output

{'C': 1, 'o': 1, 'd': 1, 'i': 2, 'n': 2, 'g': 1, 's': 1, 'f': 1, 'u': 1}


get will take a key (a character) and a default value (0). If the key is already in the dictionary, it will add 1. Otherwise it returns the default value.

## More Methods

• keys(), values() and items()
You can get a list of keys or values or both from a dictionary. For what? Well, you may know about it later.
>>> d = {'SC':16, 'KT':1608, 'An': 2608}
>>> print(d.keys())
dict_keys(['SC', 'KT', 'An'])
>>> print(d.values())
dict_values([16, 1608, 2608])
>>> print(d.items())
dict_items([('SC', 16), ('KT', 1608), ('An', 2608)])

• sorted()
sorted() is used to sort keys. Remember, it’s keys, not values.
>>> d = {'SC': 16, 'KT': 1608, 'An': 2608}
>>> sorted(d)
['An', 'KT', 'SC']


If you want to return sorted keys with their values,

>>> sorted(d.items())
[('An', 2608), ('KT', 1608), ('SC', 16)]


## Conclusion

We’re still not done with dictionaries actually. Dictionaries can be used in more cases than those of above. But we need to know about some more things to see dictionaries again. Let’s move on!