Create a 2D vector from a 1D vector in Python

Asked

Viewed 100 times

2

  • I have a vector with the following values:

    a = [10, 20, 30, 40, 50, 60, 70, 80]

  • I need to create 2D vector with these values. For example, it would have to look like this:

    b = [[10, 20], [30, 40], [50, 60], [70, 80]]

Please, could someone help me with this?

Thanks in advance, to those who can.

1 answer

2


You can do something like this:

b = [a[i:i+2] for i in range (0, len(a), 2]
  • Thank you very much. It worked correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.