-3

I have a simple problem that I'm finding problematic and I need some help with it. I want to convert two lists into data points.

example: Data: A = [1,2,3,4,5] B = [6,7,8,9,10]

Wanted output: C = [[1,6], [2,7], [3,8], [4,9], [5,10]]

It's like a bijective function in mathematics, but I have no idea how to implement it in code. It doesn't have to be lists. Vectors, Matrices, Arrays. Really anything will do. I have thousands of observations and I want a quick way to group them in the above fashion so I can analyze them. Right now, I'm doing a bunch of workarounds by exporting it from different software. I've tried using For loops and even indexing, but I just can't seem to get it right. New to writing code! Please help!

P.S. I'm using Python.

Ray
  • 1
  • 1
  • 1
    You should know Pythons built-in functions. Have a look at [`zip`](https://docs.python.org/3/library/functions.html#zip). – Matthias Jan 18 '22 at 07:02

2 Answers2

-1
A = [1,2,3,4,5]
B = [6,7,8,9,10]
C = list(map(lambda x, y:[x,y], A, B))
print (C)

This would give the following output

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

Hope this was helpful :)

Shubham
  • 144
  • 9
-3
import itertools
A = [1,2,3,4,5]
B = [6,7,8,9,10]

list(itertools.izip(A,B))

Result:

[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

or

with list comprehension like this :

[list(k) for k in itertools.izip(A,B)]

Result:

[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
baskettaz
  • 741
  • 3
  • 12
  • 2
    Python 3 has been out since 2008. You should be using [zip()](https://docs.python.org/3/library/functions.html#zip). [itertools](https://docs.python.org/3/library/itertools.html) no longer has `izip()`. Even in python2 plain old zip would have been a better choice here. – Mark Jan 18 '22 at 07:08
  • Thank you so much! I had no idea there were so many ways to do this! This is extremely helpful! Thanks again. – Ray Jan 18 '22 at 07:21