A dictionary is a sequence of key-value pairs separated by commas and surrounded by curly braces.
To create a dictionary, we open up a curly brace and put the data in a key-value pair separated by commas.
The basic syntax of a dictionary looks like this:
demo_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
# Creating a Dictionary with dict() method
demo_dict = dict({"key1": "value1", "key2": "value2", "key3": "value3"})
# Creating a Dictionary with each item as a Pair
demo_dict = dict([("key1", "value1"), ("key2", "value2",)])
Adding elements to a Dictionary
Addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key. While adding a value, if the key-value already exists, the value gets updated otherwise a new key with the value is added to the Dictionary.
demo_dict = {}
demo_dict["name"] = "Mike"
demo_dict["age"] = "54"
print(demo_dict) # {'name': 'Mike', 'age': '54'}
# Updating
demo_dict["age"] = "58"
print(demo_dict) # {'name': 'Mike', 'age': '58'}
# Adding multiple items
demo_dict={}
demo_dict['numbers'] = 2, 3, 4
print(demo_dict) # {'numbers': (2, 3, 4)}
Nested Dictionaries
Python lets us add a dictionary within another.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}}
print(demo_dict) # {'name': 'John', 'props': {'age': 34, 'phn': '99999'}}
Accessing an element
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}}
print(demo_dict["name"]) # John
print(demo_dict["props"]["phn"]) # 99999
Dictionary Methods
-
get()
Returns the value for the given key.
demo_dict={ "name":"Jack", "age":"24" } print(demo_dict.get("age")) # 24
-
pop()
Returns and removes the element with the given key.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} print(demo_dict.pop("name")) # John print(demo_dict) # {'props': {'age': 34, 'phn': '99999'}}
-
popitem()
Returns and removes the last key-value pair from the dictionary
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} print(demo_dict.popitem()) # ('props', {'age': 34, 'phn': '99999'}) print(demo_dict) # {'name': 'John'}
-
copy()
Returns a shallow copy of the dictionary
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} test_dict=demo_dict.copy() print(test_dict) # {'name': 'John', 'props': {'age': 34, 'phn': '99999'}}
-
fromkeys()
Creates a dictionary from the given sequence
-
items()
Return the list with all dictionary keys with values
-
keys()
Returns a list containing dictionary’s keys.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} print(demo_dict.keys()) # dict_keys(['name', 'props'])
-
values()
Returns a list of all the values available in a given dictionary.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} print(demo_dict.values()) # dict_values(['John', {'age': 34, 'phn': '99999'}])
-
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
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} demo_dict.update({"name" : "Johnny"}) print(demo_dict) # {'name': 'Johnny', 'props': {'age': 34, 'phn': '99999'}}
-
del()
The del statement removes an element from the dictionary.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} del demo_dict['name'] print(demo_dict)
-
sorted()
Returns a sorted list of keys.
demo_dict = {'6' : 'John', '3' : 'Mike', '5' : 'Jack'} print(sorted(demo_dict)) # ['3', '5', '6']
-
clear()
Removes all items from the dictionary.
demo_dict = {'name': 'John', 'props': {'age': 34, 'phn': '99999'}} demo_dict.clear() print(demo_dict) # {}
Top comments (0)