2

For example, if these are my two lists:

a=[1,2,3,4,5]
b=[6,7,8,9,10]

Then what I'm trying to do is to figure out a way to get:

c=[[1,6],[2,7],[3,8],[4,9],[5,10]]

Sorry for the probably basic question. These are numpy arrays if that makes a difference.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
rb12345
  • 29
  • 1
  • This has potentially been answered before: [enter link description here](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples-in-python) – robl Jul 11 '17 at 18:02
  • Possible duplicate of [How to merge lists into a list of tuples in Python?](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples-in-python) – CollinD Jul 11 '17 at 18:04
  • Possible duplicate of [What is the equivalent of "zip()" in Python's numpy?](https://stackoverflow.com/questions/12744778/what-is-the-equivalent-of-zip-in-pythons-numpy) – cs95 Jul 11 '17 at 18:04

9 Answers9

4

If you want a numpy array as a result, you can build it using array.T:

In [15]: a=np.array([1,2,3,4,5])

In [16]: b=np.array([6,7,8,9,10])

In [17]: np.array([a,b]).T
Out[17]: 
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

Reference: What is the equivalent of "zip()" in Python's numpy?

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
2

I don't use numpy, but maybe by using zip:

>>> a=[1,2,3,4,5]
>>> b=[6,7,8,9,10]
>>> list(zip(a,b))
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

It returns a list of tuples though.

fredtantini
  • 15,966
  • 8
  • 49
  • 55
2

One approach is using list comprehension and zip:

>>> [[i, j] for i, j in zip(a,b)]
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
1

Just use np.transpose:

>>> np.transpose([a, b])
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

If you want the result as list just call tolist() afterwards:

>>> np.transpose([a, b]).tolist()
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

This will give you what you want.

a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = [list(x) for x in zip(a, b)]
Sean Parsons
  • 724
  • 4
  • 12
  • It looks like this will return a list of tuples and not a list of lists, but one can modify it using `map`, not sure if the best way, but: `list(map(list, zip(a, b)))` works well. – Vinícius Figueiredo Jul 11 '17 at 18:05
  • 1
    @ViníciusAguiar You're right, I think the simplest and pythonic approach is just to do a list comprehension like `c = [list(x) for x in zip(a, b)]` – Sean Parsons Jul 11 '17 at 18:17
0
d = []
for i in range(0, 5):
    d.append([a[i], b[i])

Is a simple way to create a new 2D list with element pairs. Using the zip() function as others have pointed out is also viable.

ajax992
  • 917
  • 9
  • 16
0

There are multiple ways to do this.

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

If you just need to access the elements and not modify them, you can use the zip function:

zip(a, b)
>[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

If you actually need a list of lists, then you can use a list comprehension:

[[a[i], b[i]] for i in range(len(a))]
>[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]

And finally, if you need a numpy array as a result, use the Transpose function:

import numpy as np
np.concatenate([[a], [b]]).T
>array([[1, 6],
        [2, 7],
        [3, 8],
        [4, 9],
        [5, 10]])
victor
  • 1,573
  • 11
  • 23
0

I'm pretty sure there is an easier or more pythonic way than this.

c = [list(x) for x in zip(a,b)]

This outputs a list of lists, instead of just doing list(zip(a,b) that outputs a list of tuples. This combines list comprehension and zip

Also avoids the tuple unpacking of [[i,j] for i,j in zip(a,b)]

not sure whats more efficient though

zip(*iterables) Make an iterator that aggregates elements from each of the iterables.

https://docs.python.org/3/library/functions.html#zip

fcsr
  • 921
  • 10
  • 17
0

with no libraries, you could use:

a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = [[a[i],b[i]] for i in range(len(a))]
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54