Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s discuss various ways of accessing all the keys along with their values in Python Dictionary.
Method #1: Using in operator Most used method that can possibly get all the keys along with its value, in operator is widely used for this very purpose and highly recommended as offers a concise method to achieve this task.
Python3
# Python3 code to demonstrate # to get key and value # using in operator # initializing dictionary test_dict = { "Geeks" : 1 , "for" : 2 , "Lazyroar" : 3 } # Printing dictionary print ( "Original dictionary is : " + str (test_dict)) # using in operator to # get key and value print ( "Dict key-value are : " ) for i in test_dict: print (i, test_dict[i]) |
Original dictionary is : {'Lazyroar': 3, 'for': 2, 'Geeks': 1} Dict key-value are : Lazyroar 3 for 2 Geeks 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using list comprehension This method also uses a method similar to the above method, just binds the logic into one list and returns the key-value pairs of dictionary as tuples of key and value in the list.
Python3
# Python3 code to demonstrate # to get key and value # using list comprehension # initializing dictionary test_dict = { "Geeks" : 1 , "for" : 2 , "Lazyroar" : 3 } # Printing dictionary print ( "Original dictionary is : " + str (test_dict)) # using list comprehension to # get key and value print ( "Dict key-value are : " ) print ([(k, test_dict[k]) for k in test_dict]) |
Original dictionary is : {'Geeks': 1, 'for': 2, 'Lazyroar': 3} Dict key-value are : [('Geeks', 1), ('for', 2), ('Lazyroar', 3)]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using dict.items() items(), in dictionary iterates over all the keys and helps us to access the key-value pair one after the another in the loop and is also a good method to access dictionary keys with value.
Python3
# Python3 code to demonstrate # to get key and value # using dict.items() # initializing dictionary test_dict = { "Geeks" : 1 , "for" : 2 , "Lazyroar" : 3 } # Printing dictionary print ( "Original dictionary is : " + str (test_dict)) # using dict.items() to # get key and value print ( "Dict key-value are : " ) for key, value in test_dict.items(): print (key, value) |
Original dictionary is : {'Lazyroar': 3, 'for': 2, 'Geeks': 1} Dict key-value are : Lazyroar 3 for 2 Geeks 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #4: Using enumerate() Python also offers enumerate() with helps to iterate over all kinds of containers, be it dictionary or a list. The power of this function can also be utilized to perform this task. It also additionally helps to access the named index of position of the pair in dictionary.
Python3
# Python3 code to demonstrate # to get key and value # using enumerate() # initializing dictionary test_dict = { "Geeks" : 1 , "for" : 2 , "Lazyroar" : 3 } # Printing dictionary print ( "Original dictionary is : " + str (test_dict)) # using enumerate() to # get key and value print ( "Dict key-value are : " ) for i in enumerate (test_dict.items()): print (i) |
Original dictionary is : {'Lazyroar': 3, 'Geeks': 1, 'for': 2} Dict key-value are : (0, ('Lazyroar', 3)) (1, ('Geeks', 1)) (2, ('for', 2))
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using for loop and items() method
Python3
# Python3 code to demonstrate # to get key and value # using for loop and items() method # initializing dictionary test_dict = { "Geeks" : 1 , "for" : 2 , "Lazyroar" : 3 } # Printing dictionary print ( "Original dictionary is : " + str (test_dict)) # Initializing an empty list key_value_pairs = [] # Using a for loop to iterate over the items in the dictionary # and append each key-value pair to the list for key, value in test_dict.items(): key_value_pairs.append((key, value)) # Printing the key-value pairs print ( "Dict key-value are : " ) for pair in key_value_pairs: print (pair) |
Original dictionary is : {'Geeks': 1, 'for': 2, 'Lazyroar': 3} Dict key-value are : ('Geeks', 1) ('for', 2) ('Lazyroar', 3)
Time complexity of O(n), where n is the number of key-value pairs in the dictionary,
The auxiliary space complexity of O(n) to store the list of key-value pairs.