Sunday, December 29, 2024
Google search engine
HomeLanguagesPython Dictionary Methods: Clear, Copy, Pop, Get, etc

Python Dictionary Methods: Clear, Copy, Pop, Get, etc

In the previous tutorial, you learned how to use in-built lists methods in your Python program. But, In this post, you will learn python built-in dictionary methods/functions.

If you want to know more about python dictionary click here.

Python Dictionary built-in Functions/Methods

Python provides many in-built methods/functions for the dictionary, which is works with python dictionary datatype. And you can modify and manipulate the python dictionary by using built-in dictionary methods/functions of python.

We have provided a list below. Almost all built-in dictionary methods/functions python are there:

Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary’s keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary

Clear Dictionary Python

The clear() method is used to remove all the elements/items from the python dictionary.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

my_dict.clear()

print(my_dict)

#output { }

Python Copy dictionary

The copy() method is used to a copy of the specified python dictionary.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.copy()

print(x)

#output {'name': 'john', 'age': '28'}

Dictionary fromkeys Method Python

The fromkeys() method used to a dictionary with the specified keys and the specified value.

x = ('key1', 'key2', 'key3')
y = 0

my_dict = dict.fromkeys(x, y)


print(my_dict)

#{'key1': 0, 'key2': 0, 'key3': 0}

Python dictionary get method

The get() method is used to get the value of the item with the specified key.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.get('name')

print(x)

#output john

Python dictionary items method

The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.items()

print(x)

#output dict_items([('name', 'john'), ('age', '28')])

Python dictionary keys method

The keys() method returns a view object. The view object contains the keys of the dictionary, as a list.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.keys()

print(x)

#output dict_keys(['name', 'age'])

Dictionary pop method python

The pop() method is used to remove the specified item from the dictionary.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.pop('name')

print(x)

#output john

Python Dictionary popitem() Method

The popitem() method used to remove the item that was last inserted into the dictionary.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.popitem()

print(x)

#output ('age', '28')

Python Dictionary update() Method

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

my_dict.update({"fav_color": "White"})

print(my_dict)

#output {'name': 'john', 'age': '28', 'fav_color': 'White'}

Python Dictionary values() Method

The values() method is used to return a view object. The view object contains the values of the dictionary, as a list.

# python dictionary

my_dict = {'name': 'john', 'age': '28'}

x = my_dict.values()

print(x)

#output dict_values(['john', '28'])
RELATED ARTICLES

Most Popular

Recent Comments