1

I'm new to python, and I'm trying to get new vectors from the data I have. I have several lists, for example:

list1 = [a, b, c]
list2 = [d, e, f]
list3 = [m, n, o]

Output I want

vector1 = [a, d, m]
vector2 = [b, e, n]
vector3 = [c, f, o]

The first elements of the three lists are vector 1, the second elements are vector 2, and so on.

  • Please, be more specific. Are all the list? of the same size? Are there always just 3 lists? – chrslg Oct 09 '22 at 11:18
  • Isn't this specific, these are the only three lists required for in the solutio – PythonProgrammer Oct 09 '22 at 11:25
  • @PythonProgrammer: It matters because some solutions won't work properly (dropping data) if the sizes might not match, and simplifications for *very* specific cases might not work with slightly different input sizes/formats/etc. As written, `zip` is *the* solution, but it will lose data if the inputs have uneven sizes. – ShadowRanger Oct 09 '22 at 11:33
  • I see, I assumed here that OP only wants this solution for these three lists and not any others though. – PythonProgrammer Oct 09 '22 at 11:36

2 Answers2

2

You can use zip to achieve this.

list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']
list3 = ['m', 'n', 'o']
vector1, vector2, vector3 = map(list, zip(list1, list2, list3))
bn_ln
  • 1,648
  • 1
  • 6
  • 13
  • I would like to know how this works without enumerate()? – PythonProgrammer Oct 09 '22 at 11:26
  • Looking at the code, it seems like it would just map the elements of list1 to vector1, however this isn't the case? – PythonProgrammer Oct 09 '22 at 11:29
  • 1
    `zip` iterates through each of the lists element by element (note that it will stop once it hits the end of the shortest list), and returns tuples of the corresponding elements. As the question asked for lists, I applied the `list` function to each tuple using `map`. – bn_ln Oct 09 '22 at 11:29
  • 1
    @PythonProgrammer: `zip` does pairwise iteration. So the first output from it is a `tuple` of the first item from each of its three arguments. The OP wants a `list`, so `map(list` converts the `tuple` to a `list`, and that first output is put in `vector1`. Then `zip` produces the `tuple` of all second items, which convert to `list`, which go in `vector2`, etc. – ShadowRanger Oct 09 '22 at 11:31
  • 1
    So `vector1, vector2, vector3 = zip(list1, list2, list3)` would create "vectors" as tuples instead of lists – bn_ln Oct 09 '22 at 11:31
  • Cool, I was originally trying something like ```vector1 = [item for (item, idx,item, idx, item, idx) in (enumerate(list1), enumerate(list2), enumerate(list3)]``` but it didn't work – PythonProgrammer Oct 09 '22 at 11:31
0

If all the list are of the same size, you could use numpy


M=np.array([list1, list2, list3])
vector1, vector2, vector3=M[:,0], M[:,1], M[:,2]

A more pure python way

L=[list1, list2, list3]
vector1=[L[i][0] for i in range(len(L))]
vector2=[L[i][1] for i in range(len(L))]
vector3=[L[i][2] for i in range(len(L))]

Or a more generic one

def getCol(i, *vec):
   return [vec[j][i] for j in range(len(vec)) if i<len(vec[j])]
vector1=getCol(0, list1, list2, list3)

(works with heteregeneous list size)

chrslg
  • 9,023
  • 5
  • 17
  • 31