Python Dictionaries
This article is related to all functions present inside python dictionary function
Table of contents
No headings in the article.
clear()
Removes all the elements from the dictionary
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } car.clear() print(car)
copy()
Returns a copy of the dictionary
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.copy() print(x)
fromkeys()
Returns a dictionary with the specified keys and value
x = ('car', 'company', 'fruit') y = 'apple' thisdict = dict.fromkeys(x,y) print(thisdict)
get()
Returns the value of the specified key
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.get("model") print(x)
items()
Returns a list containing a tuple for each key value pair
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.items() print(x)
keys()
Returns a list containing the dictionary’s keys
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.keys() print(x)
pop()
Removes the element with the specified key
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } car.pop("model") print(car)
popitem()
Removes the last inserted key-value pair
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } car.popitem() print(car)
setdefault()
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.setdefault("model", "CLS") print(x)
update()
- Updates the dictionary with the specified key-value pairs
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } car.update({"color": "Black"}) print(car)
values()
- Returns a list of all the values in the dictionary
car = { "brand": "Mercedes", "model": "AMG", "year": 2001 } x = car.values() print(x)