Iterate Over Dictionaries in Python Using Both Key() & Value() Function

In this blog, we will compile a Python Program to Iterate Over Dictionaries Using Both Key() & Value() Functions. Dictionaries are a convenient way to store data in key-value pairs however, accessing and iterating over these dictionaries can sometimes be tricky, especially when you want to access both the keys and values. Fortunately, Python provides a simple solution to Iterate Over Dictionaries in Python Using Both Key() & Value() Functions by allowing you to simultaneously use both functions. 

Python dictionaries are a powerful data structure that allows you to store data in key-value pairs. Often, you may need to loop through all the key-value pairs in a dictionary to perform certain operations. In Python, you can easily iterate over dictionaries using the items() function. This function returns a view object that contains the key-value pairs of the dictionary as tuples, which can then be unpacked into variables for further processing.

In this blog post, we will explore how to use both of these functions to iterate over dictionaries in Python, and we will provide a Python program that demonstrates this process.

What is Key() & Value() Function in python:

In Python, key() and value() are built-in functions that are used to recall the keys and values from a dictionary, respectively.

The key() function returns a list of all the keys present within the dictionary and the value() function returns a list of all the values associated with those keys. We use these functions when we want to manipulate or access keys or values individually.

In the following example, we will be applying these functions to iterate over the keys of the dictionary and access their values using the key as an index.

Python Program to Iterate Over Dictionaries Using Both Key() & Value() Function

# Iterate Over Dictionaries in Python Using Both Key() & Value() Function
# Access both key and value without using items()

countryAndCode = {
	'India': 'IND',
	'Afghanistan': 'AFG',
	'Canada': 'CAN',
	'Russia': 'RUS'
}

for i in countryAndCode:
# we are using a key as an index to print values from the directory
	print(i, '->', countryAndCode[i])

Code Logic and Explanation:

  • Create a dictionary

We start by creating a dictionary called countryAndCode with 4 key-value pairs which has country names as keys and their corresponding country codes as values.

  • Iterate over the dictionary

Here, we are using a for loop to iterate over the dictionary. The loop automatically iterates over the keys of the dictionary.

  • Accessing both key and value without using items()

Inside the loop, we are accessing both the key and value of the dictionary. We use the key i as an index to access the value corresponding to that key. We print the key-value pair by concatenating the key i, an arrow symbol, and the value countryAndCode[i].

  • Printing key-value pairs

Finally, we print the key-value pairs using the print() function. We concatenate the key i, an arrow symbol, and the corresponding value countryAndCode[i] to display the key-value pairs.

Output:

India -> IND
Afghanistan -> AFG
Canada -> CAN
Russia -> RUS

As we can see, this code creates a dictionary called countryAndCode that contains country names as keys and their corresponding codes as values. The for loop then iterates over the keys of the dictionary, which are the country names. Inside the loop, the key is used to access the corresponding value from the dictionary and print it. The output will print the name of each country, followed by an arrow (->) symbol, and then the country code.

For example, if we consider the first iteration of the loop, the key ‘India’ will be assigned to the variable ‘i’. Using ‘i’ as an index, the corresponding value ‘IND’ is accessed from the countryAndCode dictionary and printed along with the key ‘India’ as ‘India -> IND’. This process continues for all the keys in the dictionary, resulting in the output of all country names and their codes.

For iterating over dictionaries, other alternate methods that you can use are:

  • items() method: This method allows you to access both the keys and values of the dictionary together in a loop without needing to separately use the key() and value() functions.
  • keys() and values() methods: You can also use these methods to separately access the keys and values of the dictionary in a loop. This provides a more straightforward and readable way of accessing the keys and values. Additionally, it can be helpful if you need to perform operations on just the keys or just the values.

In this tutorial, we learn how to iterate over dictionaries in Python. Since we’re using both the key() and value() functions, our programming will be simplified. The key() function returns a list of keys in the dictionary, while the value() function returns a list of values. By using the keys as an index to access the corresponding values. This technique can be applied to a wide range of applications, from data processing to web development, making it a valuable skill for any Python programmer to learn.

Some FAQs on Iterate Over Dictionaries in Python Using Both Key() & Value()

How can I add a key-value pair to a dictionary in Python?

You can add a key-value pair to a dictionary in Python by using the syntax dictionary_name[key] = value.

What is the difference between a list and a dictionary in Python?

Lists are similar to arrays, which are declared in other languages. They do not always have to be homogeneous, making them a handy tool in Python. They are changeable, which means they can be changed even after they are created. A dictionary is an unordered collection of data values that store data values like a map, whereas other Data Types only hold a single value as an element. The dictionary contains key-value pairs to make it more optimized.

Can I add duplicate keys in a dictionary in Python?

No, you cannot have duplicate keys in a dictionary in Python. If you try to add a key that already exists, the value for that key will be overwritten.

About The Author

Leave a Reply