Iterating over a list by list in Python

I want to iterate over the list.
I want to iterate over irregularly nested lists inside a list.
Can anyone let me know how I can do this?

x = [u'sam', [['Test', [['one', [], []]], [(u'file.txt', ['id', 1, 0])]], ['Test2', [], [(u'file2.txt', ['id', 1, 2])]]], []] 
+39
source share
9 answers

This traverse generator function can be used to iterate over all values:

 def traverse(o, tree_types=(list, tuple)): if isinstance(o, tree_types): for value in o: for subvalue in traverse(value, tree_types): yield subvalue else: yield o data = [(1,1,(1,1,(1,"1"))),(1,1,1),(1,),1,(1,(1,("1",)))] print list(traverse(data)) # prints [1, 1, 1, 1, 1, '1', 1, 1, 1, 1, 1, 1, 1, '1'] for value in traverse(data): print repr(value) # prints # 1 # 1 # 1 # 1 # 1 # '1' # 1 # 1 # 1 # 1 # 1 # 1 # 1 # '1' 
+47
source

So wait, is this just a list on a list?

The easiest way is to simply use nested loops:

 >>> a = [[1, 3, 4], [2, 4, 4], [3, 4, 5]] >>> a [[1, 3, 4], [2, 4, 4], [3, 4, 5]] >>> for list in a: ... for number in list: ... print number ... 1 3 4 2 4 4 3 4 5 

Or is it something more complicated? Arbitrary nesting or something else? Let us know if there is anything else.

Also, for performance reasons, you can see how to use lists for this:

http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions

+33
source

This can also be achieved with itertools.chain.from_iterable , which will smooth out successive iterations:

 import itertools for item in itertools.chain.from_iterable(iterables): # do something with item 
+14
source

if you don't want recursion, you can try:

 x = [u'sam', [['Test', [['one', [], []]], [(u'file.txt', ['id', 1, 0])]], ['Test2', [], [(u'file2.txt', ['id', 1, 2])]]], []] layer1=x layer2=[] while True: for i in layer1: if isinstance(i,list): for j in i: layer2.append(j) else: print i layer1[:]=layer2 layer2=[] if len(layer1)==0: break 

which gives:

 sam Test Test2 (u'file.txt', ['id', 1, 0]) (u'file2.txt', ['id', 1, 2]) one 

(note that he did not look at tuples for lists because tuples are not lists. You can add a tuple to the "isinstance" method if you want to fix this)

+6
source

Sounds like you need to use recursion. Make a function to iterate through the list, and if it falls into an element that is also a list, call yourself to repeat in the member. Here's a link to something similar:

http://www.saltycrane.com/blog/2008/08/python-recursion-example-navigate-tree-data/

+4
source

If you are interested in getting all the values ​​in one list, you can use the following code:

 text = [u'sam', [['Test', [['one', [], []]], [(u'file.txt', ['id', 1, 0])]], ['Test2', [], [(u'file2.txt', ['id', 1, 2])]]], []] def get_values(lVals): res = [] for val in lVals: if type(val) not in [list, set, tuple]: res.append(val) else: res.extend(get_values(val)) return res get_values(text) 
+4
source
 x = [u'sam', [['Test', [['one', [], []]], [(u'file.txt', ['id', 1, 0])]], ['Test2', [], [(u'file2.txt', ['id', 1, 2])]]], []] output = [] def lister(l): for item in l: if type(item) in [list, tuple, set]: lister(item) else: output.append(item) lister(x) 
+2
source

Create a method to recursively iterate over nested lists. If the current item is an instance of the list, call the same method again. If not, print the current item. Here is an example:

 data = [1,2,3,[4,[5,6,7,[8,9]]]] def print_list(the_list): for each_item in the_list: if isinstance(each_item, list): print_list(each_item) else: print(each_item) print_list(data) 
+2
source

two nested loops for a loop?

  for a in x: print "--------------" for b in a: print b 

This will help if you give an example of what you want to do with lists.

+1
source

Source: https://habr.com/ru/post/1485330/


All Articles