Dictionary in Python

LEELA S
5 min readMar 4, 2022

Python dictionary is an unordered collection of items. Each item of a dictionary has a key-value pair which are separated by a colon(:) and enclosed within curly braces { }.It can be changeable and do not allow duplicate.

Creating a Dictionary

A dictionary may contain any data type and is mutable.Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable.

Syntax

Accessing Elements on a Dictionary

Dictionary is an unordered data type.Hence, we cannot use the indexing operator to access the values.We will have to use the key to access the associated value.To access data,we can either place the keys inside the square brackets[] or use the get method( ).

Example

Output

Adding and Modifying Entries to a Dictionary

a)Adding Dictionary Entries

TO add a new key value pair or to modify the values of a dictionary , we will use the assignment operator (=).Python evaluates the new entry and checks if there is a similar key in the current dictionary.If there is none,then it appends the key:value pair.If the key alreary exists,then it updates the value of existing key.

Syntax

Example

Output

{‘Name’: ‘leela’, ‘Age’:’21', ‘course’: ‘CSE’}

b)Modifying Dictionary Entries

To modify the current value , use the assignment operator (=) to specify the new value to be associated with the key

Example

Ouput

{‘Name’: ‘Renu’}

Removing or Deleting Elements from a Dictionary

To remove a key:value from a dictionary, we can use the pop( ) method which removes the pair and return the value of the given key.

a) pop( ) method

Used to remove a specified key-value pair from a dictionary and returns the value of the deleted key.

Example

Output

{‘a’: 1, ‘b’: 2, ‘c’: 3}

b)popitem( ) method

Used to remove a random key-value pair from a dictionary.The method does not take an argument and returns the deleted key-value pair.

Output

{‘a’: 1, ‘b’: 2, ‘c’: 3}

c) clear( ) method

Used to remove all the key-value pairs in a dictionary

Example

Output

{}

d) Deleting a Dictionary

The del keyword is used to delete a dictionary.

Example

Output

Python Dictionary Methods

a) Update( )

Updates a dictionary with key-value pairs from another dictionary.

Example

Output

{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4,’name’: ’amu’}

b)Item( ) method

Returns list of a dictionary’s key-value pairs.

Example

Output

dict_items([(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4)])

c) Value( ) method

Returns a list of dictionary values.

Example

Output

dict_values([1, 2, 3, 4])

d) Keys( ) method

Returns a list of dictionary keys.

Example

Output

dict_keys([‘a’, ‘b’, ‘c’, ‘d’])

e) Setdefault( ) method

Searchers for a given key in a dictionary and returns the value if found.If not, it returns the default value.

Example 1

Output

3

Example 2

Output

None

Dictionary Membership Test

We can use the membership operatoes ‘in’ and ‘not in’ to check whether a specific key exists or not on the dictionary.It is only for dictionary keys, not values.

Example

Output

True

False

True

Using built in functions with dictionary

Python has several built-in functions that can be used with dictionary to perform various tasks.

a) len( )

Returns the number of items on a dictionary.

Example

Output

4

b) sorted( )

Returns a sorted view of dictionary keys but not sort the dicyionary itself.

Example

Output

[‘a’, ‘c’, ‘d’, ‘z’]

c) Creating a dictionary with the dict( ) function

Another way of creating a dictionary is with dict( ),a built-in function.The function allows programmers to create a dictionary out of a list of tuple pairs.Each pair will have two elements that can be used as a key and value.

Example

Output

{‘cat’: ‘kitten’, ‘dog’: ‘puppy’, ‘lion’: ‘cub’}

d) Dictionary Comprehension

Dictionary comprehension is a concise way of creating a new dictionary from a python iterable.It consists of a key-value expression and a for statement enclosed in curly braces { }.

Example

Output

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

LEELA S
LEELA S

No responses yet

Write a response