Article | ape to plough

This article was first published in the public number “ape Tiangang”, reproduced please indicate the source, thank you!

preface

A Python dictionary can be as short as one key-value pair, or as long as millions. So we can’t walk through the dictionary. Because Python dictionaries are made up of a series of key-value pairs, we can iterate over all of the key-value pairs, keys, or values of the dictionary.

1. Walk through all the key-value pairs in the dictionary

Let’s start with a student dictionary:

student = {'num': '123456'.'name': 'kelvin'.'age': 18}
Copy the code

In the previous article we knew how to access any item in the Student dictionary, but what if we wanted to access all of the information in the dictionary?

We can use a for loop to iterate over the dictionary:

student = {'num': '123456'.'name': 'kelvin'.'age': 18}

for key, value in student.items():
    print("\nKey: " + key)
    print("Value: " + str(value)) Since age corresponds to a numeric type, use the STR () method to convert it to a string
Copy the code

As you can see from the code, we declare two variables key and value to store the keys and values in the key-value pair. They can be called anything.

You could even write:

for a, b in student.items():
Copy the code

It doesn’t matter, but for the sake of justification, I’m going to say key and value.

If you print(student.items()), you’ll notice that “in” is followed not by the dictionary itself, but by student.items(), which returns a list of key/value pairs. In fact, if you print(student.items()), you’ll get something like this:

dict_items([('num'.'123456'), ('name'.'kelvin'), ('age'18)]),Copy the code

You can see that this is indeed a list of key/value pairs.

Next, the for loop stores each key-value pair tuple in turn into the two specified variables (key and value).

Run the code above to traverse the Student dictionary:

Note: When iterating through the dictionary, the order in which the key-value pairs are returned may be different from the order in which they are stored. In fact, it doesn’t matter how we write programs.

2. Walk through all the keys of the dictionary

Sometimes we don’t care what the value is, we can use the keys() method, which returns a list of keys.

student = {'num': '123456'.'name': 'kelvin'.'age': 18}

for key in student.keys():
    print(key)
Copy the code

The code above prints out all the keys in the dictionary:

Keys () returns a list of keys if we try print(student.keys()). Keys () returns a list of keys.

We can even omit the keys() method, because when iterating through dictionaries, if you declare only one variable after for, Python defaults to iterating through all the keys and assigning them to that variable in turn.

Therefore, our code could also be written like this:

student = {'num': '123456'.'name': 'kelvin'.'age': 18}
for key in student:
    print(key)
Copy the code

The output is the same as above.

Python provides a very flexible syntax, and you are free to write it any way you like.

3. Iterate over all the values in the dictionary

Sometimes we just need to go through all the values in the dictionary without caring what the keys are. We can use the values() method, which returns a list of values.

If we want to print out all the values in the student dictionary:

student = {'num': '123456'.'name': 'kelvin'.'age': 18}

for value in student.values():
    print(value)
Copy the code

Run this code and it looks like this:

It does print out all the values in the Student dictionary.

The key to being able to print all the values is that the student.values() method returns a list of values.

Python iterates through the list of values and assigns the values in turn to the value variable, then prints it out.

The closing

This article focuses on three ways Python traverses dictionaries, which should be relatively simple.

I have always tried to make complex concepts easy to understand, but I may not have considered them well because of my personal ability. If you have any doubts about the content of the article, welcome to communicate with me.

Scan code to pay attention to “Monkey Tiangang”