Python Dictionary
- Dictionary is a datatype in python which works on key-value pairs.
- Data inside the dictionary is defined in form of keys and values. Each key in the dictionary has a corresponding value assigned to it.
- Dictionary is defined using curly braces{}.
- Dictionary works almost in the same way as a JSON object works.
Example of Dictionary
dict1={"name": "mradul","age": 23,"city": "indore"}
print(dict1)
Here in this example name, age and city are the keys. Each key in the dictionary is assigned a value.
Example to access all keys of Dictionary
dict1 = {"name":"mradul","age":23,"city":"indore"}
print(dict1.keys())
Example to access all values of Dictionary
dict1 = {"name":"mradul","age":23,"city":"indore"}
print(dict1.values())
Python dictionaries are a data structure used to store a collection of key-value pairs. Here are some of the most commonly used dictionary methods in Python
- clear(): This method removes all items from the dictionary.
dict1 = {"name": "John", "age": 30} dict1.clear() print(dict1) # Output: {}
- copy(): This method returns a shallow copy of the dictionary.
dict1 = {"name": "John", "age": 30} dict2 = dict1.copy() print(dict2) # Output: {"name": "John", "age": 30}
- get(): This method returns the value of a specified key. If the key is not found, it returns a default value.
dict1 = {"name": "John", "age": 30} print(dict1.get("name")) # Output: "John" print(dict1.get("gender", "Unknown")) # Output: "Unknown"
- items(): This method returns a list of key-value pairs in the dictionary.
dict1 = {"name": "John", "age": 30} print(dict1.items()) # Output: dict_items([('name', 'John'), ('age', 30)])
- keys(): This method returns a list of keys in the dictionary.
dict1 = {"name": "John", "age": 30} print(dict1.keys()) # Output: dict_keys(['name', 'age'])
- values(): This method returns a list of values in the dictionary.
dict1 = {"name": "John", "age": 30} print(dict1.values()) # Output: dict_values(['John', 30])
- pop(): This method removes and returns the value of a specified key.
dict1 = {"name": "John", "age": 30} value = dict1.pop("age") print(value) # Output: 30 print(dict1) # Output: {"name": "John"}
- popitem(): This method removes and returns the last inserted key-value pair from the dictionary.
dict1 = {"name": "John", "age": 30} item = dict1.popitem() print(item) # Output: ('age', 30) print(dict1) # Output: {"name": "John"}
- setdefault(): This method returns the value of a specified key. If the key is not found, it inserts the key with a default value.
dict1 = {"name": "John", "age": 30} value = dict1.setdefault("gender", "Unknown") print(value) # Output: "Unknown" print(dict1) # Output: {"name": "John", "age": 30, "gender": "Unknown"}
- update(): This method updates the dictionary with the key-value pairs from another dictionary.
dict1 = {"name": "John", "age": 30} dict2 = {"gender": "Male", "city": "New York"} dict1.update(dict2) print(dict1) # Output: {"name": "John", "age": 30, "gender": "Male", "city": "New York"}