- Dictionary is a data structure in python which store as a key:value pair.
- Dictionaries do not allow duplicate keys, since it uses hashing concep store keys.
Overview of most used Dictionary Methods
1. declaring a dictionary
dictionary = {}
2. Inserting Key:Value pairs
-
we have a specific syntax to do it, as shown below.
example:-
dictionary['name'] = 'ankit' print(dictionary) # output : { 'name' : 'ankit' }
3. Accessing a Value using Key
- There are two ways you can access a value using key
- In python, there is a method
.get()
which takes a key as a argument, used to get a value as shown -
.get()
will return None , if key is not found in a dictionary - The other way to access a value is using square brackets,
value = dictionary.get('name')
print(value)
#output: 'milk'
value = dictionary.get('unknown_key')
print(value)
#output : None
# other way
name = dictionary['name']
print(name)
#output: 'milk'
4. Methods used while looping a Dictionary
items() :
- The
items()
method in Python dictionaries returns a view object that contains the key-value pairs of the dictionary as tuples. - example:-
my_dict = {'apple': 2, 'banana': 3, 'cherry': 5}
for key, value in my_dict.items():
print(key, value)
#output apple 2
banana 3
cherry 5
5 .keys():
- This method will return dict_keys which are list of keys in a dictionary. we can use loops to access all the keys. example:-
keys = my_dict.keys()
for key in keys:
print(key)
#output: apple
banana
cherry
6 .values():
-
This method will return dict_values which are list of values in a dictionary. we can use loops to access all the values.
example:-
values = my_dict.values() for value in values: print(value) #output: 2 3 5 ```
7. in:
- It is a keyword which can be used to find weather the key is present or not it will return a boolean value as shown example:-
boolean = 'banana' in my_dict
print(boolean)
#output: True
boolean = 'unknown_key' in my_dict
print(boolean)
#output: False
8. update():
-
This will take another dictionary as argument and addes it to the old one
example:-
new_dict = { 'Lemon' : 10 } my_dict.update(new_dict) print(my_dict) #output: {'apple': 2, 'banana': 4, 'cherry': 5, 'Lemon':10}
9 .copy():
- This method copies the entire dictionary to the new one example:-
new_dict = my_dict.copy()
print(new_dict)
#output: {'apple': 2, 'banana': 4, 'cherry': 5, 'Lemon':10}
10. Update existing key using Square Brackets
- We can append a key value pair in to a dictionary or we can update a value using the key as shown. example:-
my_dict = {'apple': 2, 'banana': 3, 'cherry': 5}
my_dict['banana'] = 4
print(my_dict)
#output : {'apple': 2, 'banana': 4, 'cherry': 5}
11 .pop('key'):
- This method will take key as a argument and will remove the key value pair from dictionary as shown
- It will return a value of the key that was poped
- This will through KeyError if key is not found example:-
my_dict = {'apple': 2, 'banana': 3, 'cherry': 5}
my_dict.pop('banana')
print(my_dict)
#output {'apple': 2, 'cherry': 5}
12. popitem():
- This method will pop the last item that was inserted and will return a tuple of key and value
- This will return KeyError if dictionary is empty example:-
my_dict = {'apple': 2, 'banana': 3, 'cherry': 5}
item = my_dict.popitem()
print(item)
print(my_dict)
#output ('cherry', 5)
{'apple': 2, 'banana': 3}
13 .clear():
- This method will clear the dictionary and returns None example:-
my_dict.clear()
print(my_dict)
#output: {}
14 .setdefault()
- Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary example:-
my_dict = {'apple': 2, 'banana': 3, 'cherry': 5}
value = my_dict.setdefault('banana', 10)
print(value)
print(my_dict)
value = my_dict.setdefault('orange', 7)
print(value)
print(my_dict)
#0utput 3
{'apple': 2, 'banana': 3, 'cherry': 5}
7
{'apple': 2, 'banana': 3, 'cherry': 5, 'orange': 7}
Top comments (0)