Python Dictionary Methods

  • November 30, 2022
  • python
create table

Let’s look at some crucial features that are very useful when experimenting with dictionaries in Python.

Python Dictionary Methods:
Functions Description
clear()Eliminates everything from the dictionary
copy()Returns a shallow copy of the dictionary
fromkeys()A dictionary is formed using the provided sequence.
get()Returns the value for the given key
items()Return the list with all dictionary keys with values
keys()Returns a view object that shows a list of all the dictionary’s keys in the order they were added.
pop()Returns the element with the specified key and removes it.
popitem()Returns and removes the key-value pair from the dictionary
setdefault()Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary
update()Updates the dictionary with the elements from another dictionary
values()Returns a list of all the values available in a given dictionary
Example:
Dict={"name": "Coderzpy", 
    "type": "Educational", 
    "price": "free", 
    "work-style": "remote"}
print(len(Dict))
print(Dict.values())
print(Dict.items())
print(Dict.keys())
print(Dict.__contains__('name'))
Dict.clear()
print(Dict)
Output:
4
dict_values(['Coderzpy', 'Educational', 'free', 'remote'])
dict_items([('name', 'Coderzpy'), ('type', 'Educational'), ('price', 'free'), ('work-style', 'remote')])
dict_keys(['name', 'type', 'price', 'work-style'])
True
{}

Note: also read about Python Dictionary

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

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