How can I get items from multiple lists in a certain order?

52 views Asked by At

This is sort of confusing me because what I seem to need to do is get an item from one list, an item from another list, etc. and then go back to the first list and get another item, etc. I'm having trouble figuring out how to revisit the same lists multiple times.

Here's the list:

table_data = [['apples', 'oranges', 'cherries', 'banana'],
              ['Alice', 'Bob', 'Carol', 'David'],
              ['dogs', 'cats', 'moose', 'goose']]

I need to print out this data in the following format:

apples    Alice    dogs
oranges     Bob    cats
cherries  Carol   moose
banana    David   goose

Thanks!

1

There are 1 answers

0
Iain Shelvington On BEST ANSWER

zip will generate an iterator that groups elements by their position (index) in the passed iterators. In your case passing *table_data will "expand" table data into 3 lists that then can be passed to zip

for fruit, person, animal in zip(*table_data):
    print(fruit, person, animal)