# Dictionaries

<figure><img src="https://498238201-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWuHhstIreJ3jFvE4gQ3y%2Fuploads%2FPO7qtrPGdAKvZVeG6HOs%2FDictionaries_vs_lists.jpg?alt=media&#x26;token=4b6f0c3a-91e1-49ae-9c81-5168ad2c3ca2" alt=""><figcaption><p>A dictionary is a fancy list</p></figcaption></figure>

* A dictionary consists of (key,value) pairs
* The key is an immutable type(e.g. a number, a string, a tuple)
* The value can be anything
* We retrieve the value in a dictionary by using the associated key
* Dictionaries are fancy lists that are not restricted to consecutive integers for indexing
* We create dictionaries with curly braces { }
* We assign elements to and retrieve elements from dictionaries with square brackets \[key]

```
In [1]: emails={}

In [2]: emails['Duan']='duan@mit.edu'

In [3]: emails['Charlie']='charliew@mit.edu'

In [4]: emails['Stuart']='slevine@mit.edu'

In [5]: emails['Allen']='asoberan@mit.edu'

In [6]: emails.keys()
Out[6]: ['Allen', 'Charlie', 'Duan', 'Stuart']

In [7]: emails.values()
Out[7]: ['asoberan@mit.edu', 'charliew@mit.edu', 'duan@mit.edu', 'slevine@mit.edu']

In [8]: emails
Out[8]: 
{'Charlie': 'charliew@mit.edu',
 'Duan': 'duan@mit.edu',
 'Allen': 'asoberan@mit.edu',
 'Stuart': 'slevine@mit.edu'}

In [9]: emails['Duan']
Out[9]: 'duan@mit.edu'

```

* Dictionaries can be constructed from a list of (key,value) pairs (or 2-turples)from two matching lists or keys and values

```
In [10]: instructors=['Duan','Charlie','Stuart','Allen']

In [11]: email=['duan@mit.edu','charliew@mit.edu','slevine@mit.edu','asoberan@mit.edu']

In [12]: adict=dict(zip(instructors,email))

In [13]: adict
Out[14]: 
{'Charlie': 'charliew@mit.edu',
 'Duan': 'duan@mit.edu',
 'Allen': 'asoberan@mit.edu',
 'Stuart': 'slevine@mit.edu'}
```
