Categories: python

Python Dictionary Methods

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

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

23 hours ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

3 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago