Some Basic Python Dictionary Programs-1


Dictionary

Dictionary is an unordered collection of mutable values indexed by unique keys. Values in a dictionary can be of any datatype, mutable and can be duplicated, whereas keys can’t be repeated and are immutable.

Python program to extract values of Particular Key in Nested Values

dict = {"Uppercase" : {1 : "A", 2 : "B", 3 : "C", 4 : "D", 5 : "E"},
        "Lowercase" : {1 : "a", 2 : "b", 3 : "c", 4 : "d", 5 : "e"}}
key = 4

#using values() and keys() 
values1 = [value[key] for value in dict.values() if key in dict.keys()]
print(values1) 
#Output: ['D', 'd']

#using items()
values2 = [value[key] for k, value in dict.items() if key in value]
print(values2)
#Output: ['D', 'd']
  • List comprehension is a compact method that defines a list and its contents at the same time. Its basic syntax consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element. Read more: List Comprehension in Python – Explained with Examples
  • The keys() built-in dictionary method returns a list of all the keys in the dictionary.
  • The values() built-in dictionary method returns a list of all the values in the dictionary.
  • The items() built-in dictionary method returns a list of the tuple pairs of all the keys with the values in the dictionary.

Python program to convert the tuple value list to the list of the tuples

dict = {"A" : [(19, 16, 14), (11, ), (18, 6), (10, 15, 10)],
        "B" : [(8, ), (5, 1, 7), (2, 13, 3)],
        "C" : [(3, 8, 15, 6, 19, 19)],
        "D" : [(9, 6, 3), (2, 4, 6)]}

list_tup = [(key, *ele) for key, value in dict.items() for ele in value]
print(list_tup)
#Output: [('A', 19, 16, 14), ('A', 11), ('A', 18, 6), ('A', 10, 15, 10), ('B', 8), ('B', 5, 1, 7), ('B', 2, 13, 3), ('C', 3, 8, 15, 6, 19, 19), ('D', 9, 6, 3), ('D', 2, 4, 6)]>
  • The ‘*’ operator unpacks every element of an iterable to other variables dynamically.

Python program to cross-map two dictionaries

dict1 = {"BetaPython:" : [9, 6, 3],
         "Python" : [8, 11, 5, 14, 9, 14],
         "For" : [6],
         "U" : [3, 12]}
dict2 = {3 : [14],
         5 : [1, 15],
         8 : [6, 12, 1, 7, 4, 11, 7],
         11 : [7 ,12],
         14 : [9 , 4, 5]}
dict3 = {key: [v for ele in value if ele in dict2 for v in dict2[ele]] for key, value in dict1.items()}
print(dict3)
#Output: {'BetaPython:': [14], 'Python': [6, 12, 1, 7, 4, 11, 7, 7, 12, 1, 15, 9, 4, 5, 9, 4, 5], 'For': [], 'U': [14]}
  • Dictionary comprehension is similar to set comprehension with an additional requirement of defining a key. In the comprehension expression key and values are separated by a colon ‘:’.

Python program to column mapped the tuples to the dictionary items

tup = (((1, 'I'), (2, 'II'), (3, 'III'), (4, 'IV')), (('A', 'a'), ('B', 'b'), ('C', 'c'), ('D', 'd')))
dict = {key[i] : value[i] for key, value in zip(*tup) for i in range(len(key))}
print(dict)
#Output: {1: 'A', 'I': 'a', 2: 'B', 'II': 'b', 3: 'C', 'III': 'c', 4: 'D', 'IV': 'd'}
  • The zip() built-in method returns an iterator of tuples, grouping the similar index of multiple sequences so that they can be used as a single entity.

Python program to pair the minimum value for the similar dictionary keys

list1 = [2, 4, 5, 8, 5, 2]
list2 = [3, 7, 3, 5, 7, 1]
dictionary = dict(sorted(zip(list1, list2),  key = lambda ele: -ele[1]))
print(dictionary)
#Output: {4: 7, 5: 3, 8: 5, 2: 1}
  • The lambda function is a function that is defined without a name. It can have any number of arguments but only one expression as it is a small function having a single line of code. The expression is evaluated and returned.
  • The sorted() built-in method takes an iterable and returns a sorted list containing the elements from the iterable, without modifying the original iterable. The key parameter defines the base criteria for sort. Read More: Sorting in python
  • The dict() built-in method takes iterables and creates a new dictionary from its elements.

Related Posts-

Leave a Comment

%d bloggers like this: