dictionary functions in python with examples (2024) - Python

dictionary functions in python with examples (2024)

Hello Guys Welcome to our new blog. In this blog we will learn dictionary functions in python with examples. Here you will also get the answers of the following queries

  • Dictionary functions in python with examples
  • Dictionary functions and method()
  • What are the functions of dictionary in python
  • Dictionary methods
  • Dictionary functions in python class 11
  • Built in dictionary function in python
  • dictionary function complete with examples

For Python PDF Whatsapp Me on the number-9235740454

Dictionary Functions and Method()

Here we will learn what are the built in function available in python. These are given below-

1)len()-

The function is used to calculate length of the dictionary in terms of number of elements.

Syntax

len(<dictionary name>)

Example

cricket-(1:”Sachin”, 2: “Dhoni”, 3: “Virat”, 4: “Rohit”, 5″Rahane”)

print(len(cricket))

This example will print number of elements in the Dictionary ‘cricket’ as its length. The result it will print will be 5.

2) clear()

This function is used to remove all the elements from the dictionary. It is a dictionary function so it has to be called by dictionary name.

Syntax

<dictionary name>.clear()

Example

cricket.clear()

This example will delete all the elements from the dictionary ‘cricket’.

What is a dictionary in Python?

3) get()

This function is used to obtain the value of the given key. This function takes two arguments, first of which is the key whose value is to be obtained and second is a message given as string. If the key specified in the function as argument does not exist in the dictionary it will display given message.

Syntax

<dictionary name>.get(<key>, <message>)

Example

cricket.get (6, “Key does not exist!!”)

This example will search the key 6 in the dictionary and print its corresponding value if key exist in the dictionary. In case of key 6 does not exist in the dictionary it will display message “Key does not exist!!” on screen.

4) has_key()

This function is used to check whether the given key exists in the Dictionary or not. It is a Boolean function so it returns True or False. If dictionary has the key specified in the function it returns True otherwise False.

Syntax

<dictionary name>.has_key(<key>)

This example will search the key 6 in the dictionary and if found it will print True or else False.

Dictionary operations in python with examples

5) items ()

This function is used to obtain all the items (key, value in the Dictionary as a sequence of Each key,value pair will become a tuple and this function will create a List of all tuples;

Syntax

<dictionary name>.items()

 Example

cricket.items()

This example will print all the items of the dictionary ‘cricket’ as follows: dict_items([(1, ‘Sachin’), (2, ‘Dhoni’), (3, ‘Virat’), (4, ‘Rohit’), (5, ‘Rahane’)])

6) keys()

This function is used to obtain all the keys from the ‘key: value’ pair in the Dictionary. T function returns only keys and in the form of List.

Syntax

<dictionary name>.keys()

Example

cricket.keys()

This example will print all the keys of the dictionary ‘cricket’ as follows;

dict_keys([1, 2, 3, 4, 5])

7) values()

This function is used to obtain all the values from the ‘key: value’ pair in the Dictionary. The function returns only values and in the form of List.

Syntax

<dictionary name>.values()

Example

cricket.values()

This example will print all the values of the dictionary ‘cricket’ as follows:

dict_values([‘Sachin’, ‘Dhoni’, ‘Virat’, ‘Rohit’, ‘Rahane’])

 8) update()

This function is used to update a dictionary by adding elements from another dictionary. Addi or replaces key; value pairs from the new dictionary into the original.

Syntax

<new dictionary>.update(<original dictionary>)

Example

players.update(cricket)

This example will add elements of ‘players’ dictionary into ‘cricket’ dictionary.

Subscribe Our Youtube Channel For Full Python Class by click on the below link

https://www.youtube.com/@olevelguruji

Also Check Out Latest Uploads

tuple operations in python with examples

What is a dictionary in Python?

For Python PDF Whatsapp Me on the number-9235740454

Leave a Comment