0

I have 5 lists and I want to iterate through each one, set it to a variable called current, so my output should be printing current, where current is the new list that is printed every time

One = [1,2,3]
Two = [4,5,6]
Three = [7,8,9]
Four = [10,11,12]
Five = [13,14,15]

Output:

[1,2,3]

[4,5,6]

[7,8,9]

[10,11,12]

[13,14,15]

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
zebob1
  • 1
  • 1
  • 3
    Post code you tried to solve the same. –  Aug 16 '17 at 17:05
  • Can you be more specific as to why you need to save it to a variable called `current` if all you need is to print the lists? – Miket25 Aug 16 '17 at 17:28
  • 2
    Possible duplicate of [How to print list elements (which are also lists) in separated lines in Python](https://stackoverflow.com/questions/36490752/how-to-print-list-elements-which-are-also-lists-in-separated-lines-in-python) – Eskapp Aug 16 '17 at 17:38

1 Answers1

0

You should put them all in one big list and then loop through each of them:

big_list = [[1,2,3],
           [4,5,6],
           [7,8,9],
           [10,11,12],
           [13,14,15]]

for current in big_list:
    print(current)

You can also use a more advanced feature: join. This will simplify the print statement:

big_list = [[1,2,3],
           [4,5,6],
           [7,8,9],
           [10,11,12],
           [13,14,15]]

print('\n'.join([str(current) for current in big_list]))

Output of both:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10, 11, 12]
[13, 14, 15]
bendl
  • 1,583
  • 1
  • 18
  • 41
  • what if I want to return that value of current after every iteration but then go back into the loop and continue until every list in big_list is returned – zebob1 Aug 16 '17 at 17:21
  • What do you mean by return? In the sense of the python keyword `return`, you cannot go back to a `return` statement, and it wouldn't make sense to do so. Do you maybe mean that you want to call a function each time? – bendl Aug 16 '17 at 17:22
  • yeah I want to call the function each time and use the list of current for another operation, then continue the loop and update the list of current for the next operation – zebob1 Aug 16 '17 at 17:25
  • This you would simply call the function you define yourself by passing in `current`. My example does this already, because `print` is, in fact, a function. So just change `print` to `name_of_your_function` and define your function and you should be good. – bendl Aug 16 '17 at 17:28
  • Your advice helped and I was able to solve what I needed... Thank you bendl – zebob1 Aug 16 '17 at 22:57