0

Imagine the following arrays:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = [9, 10, 11, 12]
list4 = [13, 14, 15, 16]

I want to combine the arrays so that it looks like:

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

Is there a quick way of turning an arbitrary amount of arrays and arbitrary amount of elements within the arrays into an array of array format? Furthermore, imagine that list1, 2, 3, and 4 are not connected in any way so I cannot just loop through like:

for list in lists:
> finalArray.append(list[n])

I feel like this is some matrix manipulation but I have not worked with matrices very much. Can someone point me in a direction?

Louis Ye
  • 134
  • 1
  • 13
  • 1
    These are `list` objects, not array objects. In any case, *why is it the case that there is an "unknown amount of variables"*? How are you getting into this position. You should be able to tell exactly the number of variables from your source code. – juanpa.arrivillaga Apr 02 '20 at 19:33
  • I meant to say a large amount of variables. I believe that I can get the length by just using len(array). – Louis Ye Apr 02 '20 at 19:35
  • Where are these "unknown amount of variables" coming from? Did you create them? In that case, you should have created a list of lists to begin with. This kind of smells like an XY-Problem - where you're asking for the solution to something which you think is the problem, when the problem really lies elsewhere. Can you be more concrete about where these variables are coming from and what exactly you're doing? – Paul M. Apr 02 '20 at 19:35
  • 1
    Then just `my_list = [list1, list2, ...]` by hand. – juanpa.arrivillaga Apr 02 '20 at 19:35
  • I think the more appropriate term is "arbitrary" rather than "unknown" – Kexus Apr 02 '20 at 19:36
  • No, I want my_list to be [[list1[0], list2[0], list3[0]...], [list1[1], list2[1], list3[1]...], ...] – Louis Ye Apr 02 '20 at 19:37
  • At *some point* someone wrote `list1 = ` and `list2 = ` and so forth. If there are too many to work with at this point, then that's a clear sign this code should be refactored to work with a list of lists to begin with. – juanpa.arrivillaga Apr 02 '20 at 19:38
  • 1
    @LouisYe ah, I see. OK, but still, you need to put those lists in a list or some sequence/iterable to begin with. You can then do `final = [list(t) for t in zip(*list_of_lists)]`. The matrix terminology you were looking for is *transpose*. More succinctly, perhaps, `list(map(list, zip(*list_of_lists)))` – juanpa.arrivillaga Apr 02 '20 at 19:39

2 Answers2

0

You should never be using this method. There might be a hacky workaround but 99.9% of the time you will want to have them stored in a list beforehand. Anyway, you should never have an unknown number of variables. If the user is entering data, store it in an array to start with. If this data is hard-coded, you should know how many there will be.

Theo
  • 613
  • 4
  • 22
  • I changed it to arbitrary – Louis Ye Apr 02 '20 at 19:41
  • @LouisYe you shouldn't have an arbitrary number of variables in your code. You should be using a *container* like a list or a dict to begin with. – juanpa.arrivillaga Apr 02 '20 at 19:41
  • @juanpa.arrivillaga I changed it to elements instead of variables. Maybe that was the confusion. I am pulling information from an excel file and took them out of data frame format and putting them into lists. I have a lot of list and each list has a lot of elements. I just needed to transpose it. – Louis Ye Apr 02 '20 at 19:45
  • @LouisYe well if you are using a data-frame you might as well just transpose the dataframe... `df.T` In any case, there is a linked duplicate and I reproduced the answer in a comment above for convenience – juanpa.arrivillaga Apr 02 '20 at 19:48
  • 1
    @LouisYe How are you pulling an unknown number of lines out of an Excel spreadsheet and then assigning them each to a uniquely named variable? – Theo Apr 02 '20 at 19:51
  • @Theo I don't want to get into the specifics. I have 2500+ excel files (they are all stored in one folder so I loop through all files within that folder) each with a unique name. From each excel file I am grabbing a couple lines and saving that to a list that uses the same name as the excel file. So I have a large amount of uniquely named lists each with an arbitrary amount of elements. – Louis Ye Apr 02 '20 at 20:20
  • @LouisYe Okay I understand. You want to keep the original names of the files as the variable names. I'm not sure how you managed to do that, but the point is the good way to do this is with a dictionary. For example: `for file in files: filedict[file.name] = file.content`. Then you could iterate through each of the dictionary keys and append them to a list. – Theo Apr 02 '20 at 20:42
0

You can get your result in this way:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = [9, 10, 11, 12]
list4 = [13, 14, 15, 16]
tot = [list1,list2,list3,list4]

to have your result you need to transpose the matrix:

rez = [[tot[j][i] for j in range(len(tot))] for i in range(len(tot[0]))]
Claudio
  • 642
  • 3
  • 12