Adventures in Machine Learning

Mastering Dictionary Iteration and Manipulation in Python

Iterating Over a Dictionary: A Comprehensive Guide

Dictionaries are powerful data structures in Python. They consist of a key-value pair, which allows us to store and retrieve data efficiently.

In many cases, we need to iterate over a dictionary to access its contents. In this article, we will explore how to iterate over a dictionary and save results in a new dictionary.

Iterating over the Keys of a Dictionary

To iterate over the keys of a dictionary, we can use a for loop. Each iteration through the loop will give us the next key in the dictionary.

Here’s an example:

“`

cars = {“Ford”: 15000, “Toyota”: 22000, “Chevy”: 18000}

for key in cars:

print(key)

“`

Output:

“`

Ford

Toyota

Chevy

“`

In this example, we used a for loop to iterate through the keys of the `cars` dictionary. The loop printed each key on a separate line.

Iterating over the Values of a Dictionary

To iterate over the values of a dictionary, we can use the `values()` method. This method returns a list of all the values in the dictionary.

We can then use a for loop to iterate through this list and do whatever we want with each value. Here’s an example:

“`

cars = {“Ford”: 15000, “Toyota”: 22000, “Chevy”: 18000}

for value in cars.values():

print(value)

“`

Output:

“`

15000

22000

18000

“`

In this example, we used the `values()` method to return a list of all the values in the `cars` dictionary. The for loop then printed each value on a separate line.

Iterating over Both Keys and Values of a Dictionary

To iterate over both the keys and values of a dictionary at the same time, we can use the `items()` method. This method returns a list of tuples, where each tuple contains a key-value pair.

We can then use a for loop to iterate through this list and do whatever we want with each key-value pair. Here’s an example:

“`

cars = {“Ford”: 15000, “Toyota”: 22000, “Chevy”: 18000}

for key, value in cars.items():

print(key, value)

“`

Output:

“`

Ford 15000

Toyota 22000

Chevy 18000

“`

In this example, we used the `items()` method to return a list of tuples containing key-value pairs. The for loop then printed each key-value pair on a separate line.

Saving Dictionary Results in a New Dictionary

Now let’s explore how to iterate over a dictionary and save results in a new dictionary. We can achieve this by creating an empty dictionary and adding values to it inside a for loop.

Iterating Over a Dictionary and Saving Results

To iterate over a dictionary and save results in a new dictionary, we can create an empty dictionary and add values to it inside a for loop. Here’s an example:

“`

cars = {“Ford”: 15000, “Toyota”: 22000, “Chevy”: 18000}

discounted_cars = {}

for key, value in cars.items():

discounted_price = value * 0.9

discounted_cars[key] = discounted_price

print(discounted_cars)

“`

Output:

“`

{‘Ford’: 13500.0, ‘Toyota’: 19800.0, ‘Chevy’: 16200.0}

“`

In this example, we created an empty dictionary called `discounted_cars`. Inside the for loop, we calculated a discounted price for each car using the key-value pairs from the `cars` dictionary.

Finally, we added the discounted price to the `discounted_cars` dictionary using the key from the `cars` dictionary. The print statement outside the for loop displays the new `discounted_cars` dictionary.

Adding Values to New Dictionary

To add values to a new dictionary, we can use the bracket notation. Here’s an example:

“`

discounted_cars = {“Ford”: 13500.0, “Toyota”: 19800.0, “Chevy”: 16200.0}

sold_cars = {}

sold_cars[“Ford”] = 12000.0

sold_cars[“Toyota”] = 20000.0

print(sold_cars)

“`

Output:

“`

{‘Ford’: 12000.0, ‘Toyota’: 20000.0}

“`

In this example, we created an empty dictionary called `sold_cars`. Inside the code block, we added two key-value pairs to the `sold_cars` dictionary using the bracket notation.

The print statement outside the code block displays the new `sold_cars` dictionary.

Final Thoughts

In summary, iterating over a dictionary is a core feature in Python programming. It allows us to access and manipulate key-value pairs in our data structures.

We can iterate over the keys, values or both keys and values at the same time. Similarly, we can create a new dictionary and add values to it with the use of an empty dictionary and the bracket notation.

Mastering these techniques will not only significantly improve our programming skills, but it will also enable us to write more efficient code. In conclusion, iterating over a dictionary is a crucial element in Python programming that allows us to access, manipulate and save key-value pairs.

We can iterate over the keys, values or both keys and values at the same time and create a new dictionary and add values to it using an empty dictionary and the bracket notation. By mastering these techniques, we can enhance our programming skills and write more efficient code.

Applying these concepts effectively in our work will undoubtedly lead to more productive and optimized processes and solutions.

Popular Posts