A Dictionary is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a dictionary are unordered and hence it is not possible to access dictionary's element using index number. Dictionary's elements are immutable and hence not changeable. Additionally, it does not allow multiple elements with same values (no duplicate elements.)
Dictionary contains data in key-value pair. It can be created by separating it's elements by comma (,) and enclosing with curly bracket { }. Additionally, it can also be created using .
#Dictionary with multiple datatypes Info = { "name": "John", "age": 25, "city": "London" } print(Info) #Creating dictionary with constructor Info = dict(name="Marry", age=20, city="Newyork") print(Info)
{'name': 'John', 'age': 25, 'city': 'London'} {'name': 'Marry', 'age': 20, 'city': 'Newyork'}
An element of a dictionary can be accessed using key enclosed by square brackets [ ]. The same can also be achieved using get() method.
Info = { "name": "John", "age": 25, "city": "London" } print(Info["city"]) print(Info.get("name"))
London John
To modify value, assign new value using key.
Info = { "name": "John", "age": 25, "city": "London" } Info["city"] = "Paris" print(Info)
{'name': 'John', 'age': 25, 'city': 'Paris'}
The len() function can be used to find out total number of key-value pairs in the dictionary.
Info = { "name": "John", "age": 25, "city": "London" } print(len(Info))
3
This method can be used to access dictionary's keys one by one.
Info = { "name": "John", "age": 25, "city": "London" } for x in Info: print(x)
name age city
This method can be used to access dictionary's values one by one.
Info = { "name": "John", "age": 25, "city": "London" } #first method for x in Info: print(Info[x]) #second method for x in Info.(id,title_ancestor_path,title_path,content_json,last_update_time,title,content,source,state,create_at) VALUES(): print(x)
Each method gives the same result.
John 25 London
The in keyword can be used to check whether a specified key is present in the dictionary or not.
Info = { "name": "John", "age": 25, "city": "London" } if "lastname" in Info: print("Yes, 'lastname' is a key in the dictionary.") else: print("No, 'lastname' is not a key in the dictionary.")
No, 'lastname' is not a key in the dictionary.
This can be achieved by assigning values to new index key.
Info = { "name": "John", "age": 25, "city": "London" } Info["gender"] = "Male" print(Info)
{'name': 'John', 'age': 25, 'city': 'London', 'gender': 'Male'}
There are number of ways to delete elements from a dictionary:
Info = { "name": "John", "age": 25, "city": "London" } Info.pop("city") print(Info) Info = { "name": "John", "age": 25, "city": "London" } Info.popitem() print(Info)
{'name': 'John', 'age': 25} {'age': 25, 'city': 'London'}
Info = { "name": "John", "age": 25, "city": "London" } Info.clear() print(Info) Info = { "name": "John", "age": 25, "city": "London" } del Info["city"] print(Info) del Info print(Info)
{} {'name': 'John', 'age': 25} NameError: name 'Info' is not defined
The copy of a dictionary can be created using either + operator or copy() method.
Info = { "name": "John", "age": 25, "city": "London" } Info_1 = Info Info_2 = Info.copy() print(Info_1) #'Info_1' dictionary is a reference of 'Info' print(Info_2) #'Info_2' dictionary is a copy of 'Info' Info.pop("city") #deletes 'city' key from the dictionary print(Info_1) #'city' key deleted from 'Info' and 'Info_1' print(Info_2) #no change
{'name': 'John', 'age': 25, 'city': 'London'} {'name': 'John', 'age': 25, 'city': 'London'} {'name': 'John', 'age': 25} {'name': 'John', 'age': 25, 'city': 'London'}
Methods | Description |
---|---|
Deletes all elements of the dictionary | |
A | Returns a copy of the dictionary |
Dictionary | Creates a dictionary with specified keys and values |
is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a dictionary are | Returns value of the specified key |
unordered | Returns a list of tuples with each key-value pair in tuple |
and hence it is not possible to access dictionary's element using index number. Dictionary's elements are | Returns a list of all keys of the dictionary |
immutable | Removes specified key-value pair of the dictionary |
and hence not changeable. Additionally, it does not allow multiple elements with same values ( | Removes the last key-value pair of the dictionary |
no duplicate elements. | Checks specified key in the dictionary. If the key does not exist, it inserts the specified key with specified value |
) | Updates the dictionary with the specified key-value pair(s) |
Returns a list of all values of the dictionary | |
Functions | Description |
Create Dictionary | Returns total number of elements in the dictionary |
Creates dictionary using an iterable object containing key-value pairs |