How to loop through python nested lists, and methods

Python nested list traversal method

List list nested list list in python may be a multi-dimensional list, or a certain element in the list is a list list, while others are not, so how to traverse it? In fact, it is still possible to traverse through the for loop. The number of times the for loop is used can be determined according to the number of times the list is nested. For example, a two-dimensional list can be used twice, and so on. If some elements in the list are non-iterable, such as int type, then you can use the if conditional judgment to filter. The example described below is one of the ways of nesting lists among many lists in python. The traversal method is for reference only.

Nested list traversal example code

>>> list1 = [1,2,[5,6],3]
>>> for i in list1:
...     if type(i) == type([]):
...         for j in i:
...             print(j)
... 
5
6

Original: Python nested list how to traverse, and methods

Guess you like

Origin blog.csdn.net/weixin_47378963/article/details/130191852