Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to use For loops to traverse Python dictionaries

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the methods of using For loops to traverse Python dictionaries". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In Python, how do I use the "for" loop to traverse the dictionary?

Today we will demonstrate three methods and learn to traverse nested dictionaries.

Before actual combat, we need to create a dictionary of simulation data.

Dict_1 = {'Name':' Zara', 'Age': 7,' Class': 'First','Address':'Beijing'}

Method 1: iterate using For loop + index

The easiest way to traverse a dictionary in Python is to put it directly into a for loop.

Python automatically treats dict_1 as a dictionary and allows you to iterate over its key key. We can then use the index operator to get each value.

For key in dict_1: print (key, ":", dict_1 [key])

If you want to arrange the key keys alphabetically, you can use the sorted () method, as shown below.

For key in sorted (dict_1): print (key, ":", dict_1 [key])

Method 2: iterate using the .keys () + index

You can get the same result as method 1 by using the method of .keys () to return the Python object that contains the dictionary key. Similarly, it needs to be used in conjunction with the index operator.

For key in dict_1.keys (): print (key,'-->', dict_1 [key])

Method 3: iterate with .items ()

In fact, the most "pythonic" and elegant way to traverse a dictionary is to use the .items () method.

Print (dict_1.items ())

To iterate over the keys and values of the transaction_data dictionary, you only need to "unpack" the two items embedded in the tuple, as shown below:

For k in dict_1.items v in dict_1.items (): print (k, ">", v)

It is important to note that k and v are only standard aliases for keys and values, but you can also choose other naming conventions.

For example, we can replace it with an and b, and we will have the same output.

For an in dict_1.items b in dict_1.items (): print (a, "-", b)

Advanced: traversing nested dictionaries

Sometimes, we come across more complex dictionaries-nested dictionaries.

So what should we do about this situation?

Dict_2 = {"num_1": {'Name':' Zara', 'Age': 7,' Class': 'First','Address':'Beijing'}, "num_2": {' Name': 'BOb',' Age': 32, 'Class':' Six','Address':'Shanghai'}, "num_3": {'Name':' Tom', 'Age': 25,' Class': 'Second' 'Address':'Wuhan'}} dict_2

To unlock the key-value pairs that belong to each nested dictionary, we can do this:

For k, v in dict_2.items (): if type (v) is dict: for nk, nv in v.items (): print (nk, "→", nv)

Use the if statement to determine whether the value value is a dictionary, and if so, use the previously mentioned method 3 and iterate with .items ().

The result of the run is as follows.

If you only want to extract some of these dictionaries, you can add conditions to the if statement.

For k, v in dict_2.items (): if type (v) is dict and k = = 'num_2': for sk, sv in v.items (): print (sk, "- >", sv)

Only the dictionary of num_2 is output, and the result is shown below.

That's all for "what are the ways to use For loops to traverse Python dictionaries?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report