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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

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

10 months ago

Minimum Cost to Paint Houses with K Colors

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

10 months ago

Longest Absolute Path in File System Representation

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

10 months ago

Efficient Order Log Storage

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

11 months ago