Python – Dictionary

Python – Dictionary

In Python, Dictionary is an unordered, indexed collection of data values used to store data in the form of keys and values. Keys must be unique whereas values can be repeated. Only immutable data types like string, tuple, int, etc. can be used as keys.

Creating a Dictionary:

a) Using a { }: Dictionary can be created using curly brackets, serparated by comma.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

print(type(fruits))

# Output
print(fruits)

Output:

<class 'dict'>
{1: 'apple', 2: 'banana', 3: 'grapes'}

b) Using a dict() function: Dictionary can be created using dict() built-in function.

# Creating a Dictionary
fruits = dict()

print("empty dictionary:",
       fruits)

fruits[1] = "apple"
fruits[2] = "banana"
fruits[3] = "grapes"

print(fruits)
empty dictionary: {}
{1: 'apple', 2: 'banana', 3: 'grapes'}

Accessing Elements in a Dictionary:

a) Using [ ]:Elements can be accessed in a dictionary by using key name in square brackets.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Accessing element with key name- 1
x = fruits[1]

# Output
print(x)

Output:

apple

b) Using get() method.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana', 
          3: 'grapes'}

# Accessing element with key name- 1
x = fruits.get(1)

# Output
print(fruits)

Output:

apple

Loop through a Dictionary:

There are many ways in which we can loop through a dictionary depending on the what we required.
a) Loop through a Dictionary-keys:

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i in fruits:
    print(i)

Output:

1
2
3

Using keys() method:

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i in fruits.keys():
    print(i)

Output:

1
2
3

b) Loop through a Dictionary-values:

# Creating a Dictionary
fruits = {1: 'apple',
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i in fruits:
    print(fruits[i])

Output:

apple
banana
grapes

Using values() method:

# Creating a Dictionary
fruits = {1: 'apple',
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i in fruits.values():
    print(i)

Output:

apple
banana
grapes

c) Loop through both keys-values of Dictionary:

# Creating a Dictionary
fruits = {1: 'apple',
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i in fruits:
    print(i,fruits[i])

Output:

1 apple
2 banana
3 grapes

Using items() method: To print keys and values of the dictionary simultaneously we use items() method.

# Creating a Dictionary
fruits = {1: 'apple',
          2: 'banana',
          3: 'grapes'}

# Looping through the dictionary
for i,j in fruits.items():
    print(i,j)

Output:

1 apple
2 banana
3 grapes

Adding Elements in a Dictionary:

To add an item in the dictionary we use new index key and assign value to it.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Adding new item 
fruits[4] = 'cherry'

# Output
print(fruits)

Output:

{1: 'apple', 2: 'banana', 3: 'grapes', 4: 'cherry'}

Removing Elements from a Dictionary:

There are many methods to remove an item from the dictionary:

a) Using pop() method: This method is used to remove specified item. Key name is provided of the item, you wish to remove.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana', 
          3: 'grapes', 
          4: 'cherry'}

# Removing item 
fruits.pop(4)

# Output
print(fruits)

Output:

{1: 'apple', 2: 'banana', 3: 'grapes'}

b) Using popitem() method: This method removes the last inserted item.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Removing item 
fruits.popitem()

# Output
print(fruits)

Output:

{1: 'apple', 2: 'banana'}

c) Using del keyword: This keyword can be used to remove an specified item as well as the entire dictionary.

# Creating a Dictionary
fruits = {1: 'apple', 
          2: 'banana',
          3: 'grapes'}

# Removing item 
del fruits[1]

# Output
print(fruits)

Output:

{2: 'banana', 3: 'grapes'}

Length of the Dictionary:

len() Built-in function is used to calculate the length of the dictionary.

# Creating a Dictionary
fruits = {1: 'apple',
          2: 'banana',
          3: 'grapes'}

# Output
print(len(fruits))

Output:

3

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds