Dictionary

Dictionary is an unordered set of key-value pairs where the keys are unique. Dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples.


In [3]:
dict1 = {'id': 1, 'name':'John Doe', 'email':'john.doe@example.org','salary':14000.00}
dict1


Out[3]:
{'email': 'john.doe@example.org',
 'id': 1,
 'name': 'John Doe',
 'salary': 14000.0}

In [4]:
dict1['name']


Out[4]:
'John Doe'

In [5]:
dict1['email']='john.doe@example.com'

In [6]:
dict1


Out[6]:
{'email': 'john.doe@example.com',
 'id': 1,
 'name': 'John Doe',
 'salary': 14000.0}

In [24]:
type(dict1)


Out[24]:
dict

In [25]:
str(dict1)


Out[25]:
"{'id': 1, 'name': 'John Doe', 'email': 'john.doe@example.com', 'salary': 14000.0}"

In [7]:
list(dict1.keys())


Out[7]:
['id', 'name', 'email', 'salary']

In [8]:
list(dict1.values())


Out[8]:
[1, 'John Doe', 'john.doe@example.com', 14000.0]

In [9]:
sorted(list(dict1.keys()))


Out[9]:
['email', 'id', 'name', 'salary']

In [43]:
del(list)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-0fc84f4d1c3e> in <module>()
----> 1 del(list)

NameError: name 'list' is not defined

In [14]:
dict2 = dict([('id',2),('name','Jack Jill'),('salary',15500)])
dict2


Out[14]:
{'id': 2, 'name': 'Jack Jill', 'salary': 15500}

In [23]:
dict(id=3, salary=25000)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-e0bf2f8c9570> in <module>()
----> 1 dict(id=3, salary=25000)

TypeError: 'list' object is not callable

In [16]:
dict = [dict1,dict2]
dict


Out[16]:
[{'email': 'john.doe@example.com',
  'id': 1,
  'name': 'John Doe',
  'salary': 14000.0},
 {'id': 2, 'name': 'Jack Jill', 'salary': 15500}]

In [17]:
dict[1]['name']


Out[17]:
'Jack Jill'

Dictionary Comprehension


In [18]:
{x: x**2 for x in range(10) if x%2 == 0}


Out[18]:
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

Looping Techniques


In [29]:
people = {'name':'John','email':'john@example.com'}
for k,v in people.items():
    print(k, v)


name John
email john@example.com

In [31]:
for key in people:
    print(people[key])


John
john@example.com

In [32]:
for value in people.values():
    print(value)


John
john@example.com

In [33]:
for key in people.keys():
    print(key)


name
email

In [47]:
__builtins__.list
countries = ["USA","England","France","India","Japan"]
capitals = ["Washington","London","Paris","New Delhi","Tokyo"]
countries_dict = dict(zip(countries,capitals))
print(countries_dict)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-e72d4a649579> in <module>()
      2 countries = ["USA","England","France","India","Japan"]
      3 capitals = ["Washington","London","Paris","New Delhi","Tokyo"]
----> 4 countries_dict = dict(zip(countries,capitals))
      5 print(countries_dict)

TypeError: 'list' object is not callable

In [ ]: