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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago