Python Dictionary

A python dictionary is ordered data type i.e. with heterogeneous data containing key and values. Dictionaries are enclosed in brackets {} with items comma separated and the item has a key: value pair.

How to create a dictionary in Python?

Dictionary in Python is created using curly brackets {} with the items in the dictionary be separated by a comma (,).  A dictionary can have any number of items and can be of different data types. To have an idea of python data types, please refer to this tutorial:- Python Datatypes.

Let’s explore some of the examples of the dictionary in the following example:-

The output of the above program is:-

{‘name’: ‘John Doe’, ‘roll’: 12345, ‘address’: ‘New York’}
Mixed keys: {‘name’: ‘John’, 1: [1, 2, 3]}

How to access elements in a dictionary in Python?

Dictionary uses keys to access elements. We can either use a square bracket with keys inside or get() method to get an item with matching key. get() method is a safer way as it doesn’t throw error, unlike the square bracket method.

Below is an example of dictionary with simple operations:-


John Doe

How to change or add elements in a dictionary in Python?

Since dictionaries are mutable data types, you can add, edit or delete every item in a dictionary. You can use a particular key update the value of the dictionary in that position.

It gives the following output:-
{‘name’: ‘John Doe’, ‘age’: 27}
{‘name’: ‘John Doe’, ‘age’: 27, ‘address’: ‘New York’}

 How to delete or remove elements from a dictionary in Python?

We can use pop() method to remove the item from the dictionary with a particular index. popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. All the items can be removed at once using the clear() method. There is also the availability of del() method as in the list.

Current Dictionary:- {‘name’: ‘John Doe’, ‘age’: 29}
Updated Dictionary:- {‘age’: 29}

How to iterate over a Dictionary in Python?

A dictionary can be iterated using for loop. Let us see the following example:-

name : John Doe
age : 29

What are Python Dictionary Operations?

inMembership operator to check if a key exists in a dictionary
not inMembership operator to check if a key does not exist in a dictionary

What are Python Dictionary Built-in functions?

Python provides the following built-in functions which can be used with the dictionaries.

all()Return True if all keys of the dictionary are true (or if the dictionary is empty).
any()Return True if any key of the dictionary is true. If the dictionary is empty, return False.
len()Return the length (the number of items) in the dictionary.
cmp()Compares items of two dictionaries.
sorted()Return a new sorted list of keys in the dictionary.

What are Python Dictionary built-in methods?

Python provides various methods that can help you perform the various operations in a dictionary. Following are some of the built-in dictionary methods:-

clear()Remove all items from the dictionary.
copy()Return a shallow copy of the dictionary.
fromkeys(seq[, v])Return a shallow copy of the dictionary.
get(key[,d])Return the value of a key. If the key does not exist, return d (defaults to None).
items()Return a new view of the dictionary’s items (key, value).
keys()Return a new view of the dictionary’s keys.
pop(key[,d])Remove the item with a key and return its value or d if the key is not found. If d is not provided and the key is not found, raises KeyError.
pop(key[,d])Remove and return an arbitrary item (key, value). Raises KeyError if the dictionary is empty.
pop(key[,d])If a key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None).
update([other])Update the dictionary with the key/value pairs from other, overwriting existing keys.
values()Return a new view of the dictionary’s values


SHARE Python Dictionary

You may also like...

Leave a Reply

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