1

Say I have multiple lists like:

[0,15678,27987,28786]
[1,12456,26789,30006]
[2,15467,29098,24567]

How would I go about zipping each entry in each list into a separate list? aka I need to be able to take 0,1,2, etc and put it into a list. Specifically, I want to know if there is an easy way to do this without giving each list a specific name.

John Smith
  • 23
  • 4
  • Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – Tomerikoo Jun 28 '21 at 11:03
  • What is the result you are expecting to have? a list of lists? or one long list comprised of the members of the shorter lists? Can you specify the result you expect for the example you provided? – Almog-at-Nailo Jun 28 '21 at 12:57

2 Answers2

1

Just use zip:

list(zip([0,15678,27987,28786],[1,12456,26789,30006], [2,15467,29098,24567]))

From your code, you can collect the data from the reader with:


with open('excelfile.csv','r',newline='') as inp:
    r = csv.reader(inp,delimiter=',')
    my_data = list(r)

The just unpack it in zip:

list(zip(*my_data))
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • @JohnSmith, just collecte the lines into a list. Expand your question with all the data so I can update the answer :) – Netwave Jun 28 '21 at 11:57
  • @JohnSmith consider upvoting and accepting the answer if it was helpful for you. – Netwave Jun 30 '21 at 14:02
1

You can use zip() with list unpacking

x=[[0,15678,27987,28786],
[1,12456,26789,30006],
[2,15467,29098,24567]]

print(list(zip(*x)))

#= Output: [(0, 1, 2), (15678, 12456, 15467), (27987, 26789, 29098), (28786, 30006, 24567)]