Python Dictionary Methods
Rushikesh J.
QA Automation Engineer @Vinz Global | Robot Framework | Manual Testing, Automation Testing | Python | Selenium | GIT | JIRA | Immediate Joiner | API Testing Using Postman | Jenkins
Python Dictionary Method 1 : clear()
Working: It used to remove all the elements from dictionary
Syntax:
dict.clear()
Example:
dic = {1: ”rushikesh", 2: ”code"}
dic.clear()
Result:
dic = {}
Python Dictionary Method 2 : copy()
Working: It used to return copy of original dictionary, it did not make any changes in original dictionary.
Syntax:
dict.copy()
Example:
dic = {1: ”rushikesh", 2: ”code"}
new = dic.copy()
Result:
new = {1: ”rushikesh", 2: ”code"}
Python Dictionary Method 3 : fromkeys()
Working: It used to return / create dictionary with key, mapping with specific value.? It take two argument, sequence and value.
Syntax:
dict.fromkeys(sequence, value)
Sequence may be any sequence, and value is used to assign to that sequence.
Example:
seq = ['b', 'c', 'd', 'e', 'f‘]
dict = dict.fromkeys(seq) # None value will be provided to every key
print(dict)
#new_dict = dict.fromkeys(seq, 0) # 0 value will be provided to every key
#print(new_dict)
Result:
{'b': None, 'c': None, 'd': None, 'e': None, 'f': None}
Python Dictionary Method 4 : get()
Working: It used to return value for specific key. If key is not exist, then None is return. It also accept default value, that will display when key is not exist.
Syntax:
dict.get(Key) # return value
#dict.get(Key, Default = None) # Return None, if key did not exist
Example:
dic = {1: ”rushikesh", 2: ”code"}
# value = dic.get(1)
value = dic.get(3) # return None
print(value)
Result:
None
Python Dictionary Method 5 : items()
Working: It used to return item form a dictionary in list form. Item is collection of key and value. Dictionary is the collection of items.
Syntax:
dict.items()
Example:
dict = {1: ”rushikesh", 2: ”code"}
dict_items = dic.items()
print(dict_items)
Result:
dict_items([(1, ‘rushikesh'), (2,’code’)])
Python Dictionary Method 6 : keys()
Working: It is used to return all the keys of dictionary in list form.
Syntax:
dict.keys()
Example:
dict = {1: ”rushikesh", 2: ”code"}
_keys = dict.keys()
print(_keys)
Result:
dict_keys([1, 2])
Python Dictionary Method 7 : pop()
Working: It is used to remove specified item from dictionary.
Syntax:
dict.pop(key, default value) # default value will be return if specified key does not exist
Example:
领英推荐
dict = {1: ”rushikesh", 2: ”code"}
element = dict.pop(2)
print(element)
Result:
code
Python Dictionary Method 8 : popitem()
Working: It is used to remove last item from dictionary.
Syntax:
dict.popitem()
Example:
dict = {1: ”rushikesh", 2: ”code"}
element = dict.popitem()
print(element)
Result:
(2,’code’)
Python Dictionary Method 9 : setdefault()
Working: It used to return value of specified key, if key does not exist then default value will be return, if default value does not provided then None is returned.
Syntax:
dict.setdefault(key, default value) # default value will be None if not provided.
Example:
dict = {1: ”rushikesh", 2: ”code"}
element = dict.setdefault(2)
#element = dict.setdefault(3) # None will return
#element = dict.setdefault(3, ‘rushikesh’) # rushikesh will return
print(element)
Result:
Code
Python Dictionary Method 10 : update()
Working: It is used to update dictionary items. It take argument either key value pair item or dictionary.
Syntax:
dict.update()
Example:
dict = {1: ”rushi", 2: ”code”}
dict1 = {3:’rushikesh’}
dict.update(dict1)
# dict.update({3 : ’rushikesh’})
print(dict)
Result:
{1: ”jafri", 2: ”code”, 3:’rushikesh’}
Python Dictionary Method 11 : values()
Working: It is used to return all the values that associated with keys in dictionary.
Syntax:
dict.values()
Example:
dict = {1: ”rushi", 2: ”code”, 3:”rushikesh”}
print(dict.values)
Result:
dict_values(['rushi', 'code', 'rushikesh'])