6

Hello I would like to create an Array from two Arrays but I do not want to create this new Array, with append() or extend().

Input arrays have the same number of rows and columns:

listone = [1,2,3]
listtwo = [4,5,6]

Outcome we expect:

mergedlist = [[1,4],[2,5],[3,6]]

It can't be done via

mergedlist = listone.append(listtwo) or mergedlist = listone.extend(listtwo)

I would like to get

mergedlist = [[1,4],[2,5],[3,6]]

How can I get the desired output?

This is a simple example to understand, real one has 14 files and 35 rows and the 61 Arrays.


For one dimensional the answer down is ok, but when you have array of list

listone = [[1,2,3],[1,2,3],[1,2,3]]
listtwo = [4,5,6]

I would like to get

result = [[1,2,3,4],[1,2,3,5],[1,2,3,6]]

when I work with merged = map(list, zip(listone, listtwo))

My result is [[[1, 2, 3], 4], [[1, 2, 3], 5], [[1, 2, 3], 6]] that is Bad
norok2
  • 21,682
  • 3
  • 59
  • 88
virtualsets
  • 363
  • 2
  • 4
  • 16
  • possible duplicate of [Python - merge items of two lists into a list of tuples](http://stackoverflow.com/questions/2407398/python-merge-items-of-two-lists-into-a-list-of-tuples) – Robert Kajic Jan 21 '14 at 14:04
  • 1
    Please don't use the term [`array`](http://docs.python.org/2/library/array.html) when you have a `list`. – Matthias Jan 21 '14 at 14:12

2 Answers2

7

Use the builtin zip function. It's exactly what you want. From the python manuals:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]

Or if you want a list of lists, instead of a list of tuples, you use zip with a list comprehension:

>>> zipped = [list(t) for t in zip(x, y)]
>>> zipped
[[1, 4], [2, 5], [3, 6]]
brianmearns
  • 9,101
  • 9
  • 50
  • 73
  • Thank you!! I check it. I can not put +1 because not have 15. But more thanks – virtualsets Jan 21 '14 at 14:04
  • Glad to help. Even if you can't vote up, you can always accept whichever answer worked for you by clicking the check mark near the score. You get a couple of reputation points for accepting an answer as well, and it encourages people to answer your questions in the future. – brianmearns Jan 21 '14 at 14:06
  • Nigel also has another answer below which should work. It uses `map` instead of list comprehensions. If it matters, you can test to see which is faster. – brianmearns Jan 21 '14 at 14:09
  • For what it's worth, I did a very quick test, and using the list comprehension is every so slightly faster than using `map`. Running each example ten million times, it took not quite one second longer to do it with `map` (about 5% longer). – brianmearns Jan 21 '14 at 14:25
  • With two dimensions elements one = [[1,4],[2,5],[3,6]] and two = [[2,5],[6,7],[6,7]] Outcome we expect: result =[[1,4,2,5],[2,5,6,7],[3,6,6,7]] it is the same? – virtualsets Jan 21 '14 at 14:37
  • You can run the python interactive shell to find out. – brianmearns Jan 21 '14 at 16:45
  • Thank you. I find http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy librarys to work with arrays. My problem is that I have 63 arrays of dimensions 32,14 ... I need more control. – virtualsets Jan 21 '14 at 17:06
  • the question asks for arrays but uses lists. How would you do this with numpy.array object? – Xitcod13 May 11 '18 at 02:09
3

Try:

listone = [1,2,3]
listtwo = [4,5,6]

merged = map(list, zip(listone, listtwo))

zip(listone, listtwo) will return a list of tuples. Since you want a list of lists you need to convert each tuple to a list. map(list, list_of_tuples) call will do exactly that.

Nigel Tufnel
  • 10,636
  • 4
  • 33
  • 31