0

Let's say i have a list of lists. i want to iterate through every index in every list, and use it in another func. So:

def func_a(self):
    for i in range(len(big_list)):
        current_list = big_list[i]
            for j in range(len(current_list):
                char_iter = iter(current_list)

def func_b(self):
    <<here i want to use the next char from iter>>
    <<how do i do it>>

Things I have tried:

  • next(func) -> Error
  • next(char_iter) - > Last Char

note: i write this funcs in a class.

2
  • So what you want func_a to do is just iterate through everything and the func_b will use each value coming out of func_a? Commented Jan 16, 2015 at 13:40
  • 1
    Note that for i in range(len(big_list)): current_list = big_list[i] is a poor way to do iteration. Much better to do for current_list in big_list Commented Jan 16, 2015 at 13:48

4 Answers 4

3

Not entirely sure what you are asking. As I understand the question, you want func_a to iterate though all the elements in all the nested lists, and make it so that other functions (particularly func_b) can reuse those.

In this case, you should loop through the lists and yield the chars, making func_a a generator function.

def func_a(self):
    for current_list in big_list:
        for char in current_list:
            yield char

You can then use it like this in func_b

def func_b(self):
    for char in func_a():
        print char

or using next:

def func_b(self):
    gen = func_a()
    print next(gen)
    print next(gen)
Sign up to request clarification or add additional context in comments.

Comments

1

This should give you the basic idea. Give more clarification if it's not enough.

big_list = [[1,2,3],[3,4]]

def func_a():
    for current_list in big_list:
        for value in current_list:
            yield value

y = func_a()


for x in y:
    print x
    if x>2:
        break

print 'broke'
for x in y:
    print x

Comments

0

You should not use index-based access, Python works best if you always just iterate, as it allows for lazy evaluation, as well as processing streams instead of random access containers.

I suggest using a generator:

def g(big_list):
    # no need for complicated indexing
    for current_list in big_list:
        # again, no need for index-based access
        for char in current_list:
            yield char


for char in g(the_big_list):
    func_b(char)

Comments

0
def func_a(self):
    return [j for i in big_list for j in i]

def func_b(self):
    flattened_big_list = func_a()

5 Comments

here func_a actually creates the entire list. I don't think that's what OP wants.
You can change those outer [...] to (...) to make it a generator.
@Joel Maybe... but its just such a nice one-liner you can do with Python list comprehensions I just had to show it off... :)
@tobias_k ah, right! But to be honest, if you really want to iterate, a generator with for ... for ... yield as suggested by others is easier to read.
@Joel made my case of list-comprehension-based list flattening semantically more sensible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.