Advertisement
Methods | Description |
---|---|
get(key) | Returns value associated with key in dictionary, else returns None. |
values() | Returns values in a dictionary. |
update() | Updates an existing dictionary. |
clear() | Clears/removes all the items in a dictionary. |
copy() | Returns copy of a dictionary. |
pop(key) | Removes a key and its value, if unsuccessful raises KeyError. |
pop(key,value) | Removes a key and its value, if unsuccessful returns value. |
popitem() | Removes an arbitrary key and its value from a dictonary, if unsuccessful raises KeyError. |
Advertisement
# Python - Dictionary Example
# Declaring a dictionary with key-value pairs
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print(dict1)
# Printing the keys
print('keys : ', dict1.keys())
{4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
keys : dict_keys([4, 3, 'two', 'one', 5])
#Python - Creating a dictionary with items using dict() function
# Creating an empty dictionary by calling dict() with no argument.
dict_ex = dict()
# Printing the first dictionary
print('First dictionary : ', dict_ex)
# Creating a dictionary
dict2 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
# Creating a dictionary by calling dict() with an existing dictionary as its argument
dict_ex2 = dict(dict2)
# Printing the second dictionary
print('Seoond dictionary : ', dict_ex2)
# Creating a dictionary by calling dict() with key:value pairs i.e. a dictionary as its arguments
dict_ex3 = dict({1:'One',
2:'Two',
3:'Three'
})
# Printing the third dictionary
print('Third dictionary : ', dict_ex3)
First dictionary : {}
Seoond dictionary : {4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Third dictionary : {1: 'One', 2: 'Two', 3: 'Three'}
# Python - Dictionary Example
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print(dict1)
# Accessing the value of key 3
print('The value of key 3 : ', dict1[3])
# Accessing the value of key 'two'
print('The value of key two : ',dict1['two'])
# Accessing the value of key 5
print('The value of key 5 : ', dict1[5])
{4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
The value of key 3 : Third
The value of key two : Second
The value of key 5 : Fifth
# Python - Dictionary Example
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print('Original dictionary : ', dict1)
# Updating value assciated with key 3
dict1[3] = 'Think'
# Updating value assciated with key 'two'
dict1['two'] = 'Positive'
# Updating value assciated with key 5
dict1[5] = 'And smile'
print('Modified dictionary : ', dict1)
Original dictionary : {4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Modified dictionary : {4: 'Fourth', 3: 'Think', 'two': 'Positive', 'one': 'First', 5: 'And smile'}
# Python - Dictionary Example
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth',
4:'Four',
'two':2
}
print('Dictionary : ', dict1)
Dictionary : {4: 'Four', 3: 'Third', 'two': 2, 'one': 'First', 5: 'Fifth'}
# Python - Iterating over elements of a Dictionary
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print('Key : Value')
# Using for loop to iterate over keys
for key in dict1:
value = dict1[key]
print(key, ':', value)
Key : Value
4 : Fourth
3 : Third
two : Second
one : First
5 : Fifth
# Python - Finding total length of a dictionary
#Creating a dictionary
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
# Printing items of dictionary
print(dict1)
# Printing total length of dictionary
print('Total length of dictionary : ', len(dict1))
{4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Total length of dictionary : 5
# Python - Modifying a dictionary using update() method.
# First dictionary
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
# Second dictionary
dict2 = {
40:'Forty',
30:'Thirty'
}
# Original dictionary
print('Original dictionary : ', dict1)
# Calling update() method to add a dictionary to another dictionary
dict1.update(dict2)
# Calling update() method to add a key:value pair to a dictionary
dict1.update({'X': 'Dead End'})
# Calling update() method to update existing values in a dictionary
dict1.update({3:'Three', 5:'Five'})
# Updated dictionary
print('Updated dictionary : ', dict1)
Original dictionary : {4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Updated dictionary : {4: 'Fourth', 3: 'Three', 'two': 'Second', 'one': 'First', 5: 'Five', 40: 'Forty', 30: 'Thirty', 'X': 'Dead End'}
# Python - Example with methods of dictionary
# First dictionary
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
# Second dictionary
dict2 = {
40:'Forty',
30:'Thirty'
}
# Original dictionary
print('Original dictionary : ', dict1)
# Calling pop(key) method to remove a key and its associated value
dict1.pop(4)
# Updated dictionary
print('Updated dictionary : ', dict1)
# Calling pop(key,value) method to remove a key and its associated value
dict1.pop('two', '2nd')
# Updated dictionary
print('Updated dictionary : ', dict1)
# Calling popitem() method to pop an arbitrary item
dict1.popitem()
# Updated dictionary
print('Updated dictionary : ', dict1)
# Printing the values in a dictionary
print('Values in dictionary : ', dict1.values())
Original dictionary : {4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Updated dictionary : {3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Updated dictionary : {3: 'Third', 'one': 'First', 5: 'Fifth'}
Updated dictionary : {3: 'Third', 'one': 'First'}
Values in dictionary : dict_values(['Third', 'First'])
Functions | Description |
---|---|
max() | Returns the maximum value of keys in a dictionary. |
min() | Returns the minimum value of keys in a dictionary. |
len() | Returs the total length of a dictionary. |
sum() | Returns sum of all keys in a dictionary. |
sorted() | Returns a sorted copy of a dictionary. |
any() | Returns true if any key in a dictionary is true, else false. |
all() | Returns true if all key in a dictionary are true, else false. |
#Python - Built-in functions to work with dictionary
# First dictionary
dict1 = {
4:'Fourth',
3:'Third',
2:'Second',
1:'First',
5:'Fifth'
}
# Second dictionary
dict2 = {
40:'Forty',
True:'Yes',
False:'No'
}
# 1 is boolean true, 0 for boolean false
# True(with capital T) is boolean true, False(with capital F) is boolean false
#Printing dictionarys
print('Dictionary1 : ', dict1)
print('Dictionary2 : ', dict2)
#Using max() function with a dictionary.
print('Maximum key in dict1 : ', max(dict1))
#Using min() function with a dictionary.
print('Minimum key in dict1 : ', min(dict1))
#Using sum() function with a dictionary.
print('Sum of all keys in dict1 : ', sum(dict1))
#Using sorted() function with a dictionary.
print('Sorted copy of dict1 : ', sorted(dict1))
#Using all() function with a dictionary.
print('all() boolean true keys in dict2 : ', all(dict2))
#Using any() function with a dictionary.
print('any boolean true key in dict2 : ', any(dict2))
Dictionary1 : {4: 'Fourth', 3: 'Third', 2: 'Second', 1: 'First', 5: 'Fifth'}
Dictionary2 : {40: 'Forty', True: 'Yes', False: 'No'}
Maximum key in dict1 : 5
Minimum key in dict1 : 1
Sum of all keys in dict1 : 15
Sorted copy of dict1 : [1, 2, 3, 4, 5]
all() boolean true keys in dict2 : False
any boolean true key in dict2 : True
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement