Published on

Dictionary in Python - 100DaysOfPython #Day 2

Authors

Python Dictionary Data Type

Python dictionary is a mapping object that maps hashable values to arbitrary objects. Below are most common functions and operations that are performed on dictionary data type in Python.

1. How to create dictionary in Python

There are many ways to create a dictionary, lets have a look to some of these methods.

  • Create Dictionary using dict comprehension
foo = {x: x ** 3 for x in range(20,30, 2)}
print(type(foo))
print(foo)
<class 'dict'>
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}
  • Create Dictionary using type constructor
foo = dict(x=1, y=2, z=3)
bar = dict([('x', 1), ('y', 2), ('z',3)])
print(type(foo), type(bar))
print(foo)
print(bar)
<class 'dict'> <class 'dict'>
{'x': 1, 'y': 2, 'z': 3}
{'x': 1, 'y': 2, 'z': 3}
  • Create Dictionary using key: value pairs
foo = {"x": 12345, "y": 2345, "z": 32123}
print(type(foo))
print(foo)
<class 'dict'>
{'x': 12345, 'y': 2345, 'z': 32123}

2. Get all keys from Python dictionary

foo = {x: x ** 3 for x in range(20,40, 2)}
print(foo)
foo_keys = list(foo)
print(foo_keys)
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952, 30: 27000, 32: 32768, 34: 39304, 36: 46656, 38: 54872}
[20, 22, 24, 26, 28, 30, 32, 34, 36, 38]

3. Get item with a specified key

fruits = {'mango': 2, 'apple': 1, 'orange': 5, 'pineapple ': 2}
print(fruits["mango"])
print(fruits["orange"])
2
5

4. Remove a key from dictionary

fruits = {'mango': 2, 'apple': 1, 'orange': 5, 'pineapple ': 2}
print(fruits)
del fruits["mango"]
print(fruits)
{'mango': 2, 'apple': 1, 'orange': 5, 'pineapple ': 2}
{'apple': 1, 'orange': 5, 'pineapple ': 2}

5. Check if key is available in dictionary

foo = {x: x ** 3 for x in range(20,30, 2)}
print(foo)
bar = {x: x ** 3 for x in range(1,10, 2)}
print(bar)

# check in foo
if 20 in foo:
    print("Key found in foo")
else:
    print("Key not found in foo")

# check in bar
if 20 in bar:
    print("Key found in bar")
else:
    print("Key not found in bar")

{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}
{1: 1, 3: 27, 5: 125, 7: 343, 9: 729}
Key found in foo
Key not found in bar

6. Remove all items from dictionary

foo = {x: x ** 3 for x in range(20,30, 2)}
print(foo)
bar = {x: x ** 3 for x in range(1,10, 2)}
print(bar)

foo.clear()
bar.clear()
print(foo)
print(bar)
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}
{1: 1, 3: 27, 5: 125, 7: 343, 9: 729}
{}
{}

7. Update a dictionary

foo = {x: x ** 3 for x in range(20,30, 2)}
bar = {x: x ** 3 for x in range(1,10, 2)}
print(foo)
print(bar)
foo.update(bar)
print(foo)
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}
{1: 1, 3: 27, 5: 125, 7: 343, 9: 729}
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952, 1: 1, 3: 27, 5: 125, 7: 343, 9: 729}

8. Remove key or last added item from the dictionary

foo = {x: x ** 3 for x in range(10,16, 2)}
bar = {x: x ** 3 for x in range(1,5, 2)}
foo.update(bar)
print(foo)
removed_item = foo.popitem()
print(removed_item)
print(foo)
removed_value = foo.pop(10)
print(removed_value)
print(foo)
{10: 1000, 12: 1728, 14: 2744, 1: 1, 3: 27}
(3, 27)
{10: 1000, 12: 1728, 14: 2744, 1: 1}
1000
{12: 1728, 14: 2744, 1: 1}

9. Iterate over a dictionary

d = {"one": 1, "two": 2, "three": 3, "four": 4}

# print keys
for i in d:
    print(i)
#print items (key, value) pair
for i in d.items():
    print(i)
one
two
three
four
('one', 1)
('two', 2)
('three', 3)
('four', 4)

10. Get reverse items from dictionary

foo = {x: x ** 3 for x in range(20,30, 2)}
print(foo)
print(list(reversed(foo)))
print(list(reversed(foo.values())))
print(list(reversed(foo.items())))
{20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}
[28, 26, 24, 22, 20]
[21952, 17576, 13824, 10648, 8000]
[(28, 21952), (26, 17576), (24, 13824), (22, 10648), (20, 8000)]