0

I have a list below. I would like to create a two or multidimensional list from one dimension normal list

x= [ light, double , motor , boolean , brakes , unit 8]

I would like to see like that

x= [ [light, double] , [motor , boolean] , [brakes , unit 8]]

in this way I can select and check the type of specific signal

The Berga
  • 3,744
  • 2
  • 31
  • 34
  • 1
    Possible duplicate of [Two dimensional array in python](https://stackoverflow.com/questions/8183146/two-dimensional-array-in-python) – Adonis Jul 05 '17 at 10:02
  • Possible duplicate of [How to define two-dimensional array in python](https://stackoverflow.com/questions/6667201/how-to-define-two-dimensional-array-in-python) – Dharma Jul 05 '17 at 11:52

3 Answers3

2

Simplest way I know of:

>>> x = [1, 2, 3, 4, 5, 6]
>>> list(zip(x[::2], x[1::2]))
[(1, 2), (3, 4), (5, 6)]

In Python 2 you can drop the list.

To help see what's going on:

>>> x[::2]
[1, 3, 5]
>>> x[1::2]
[2, 4, 6]

This is called slicing.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

You can slice and the zip the list like this:

zip(x[::2], x[1::2])

This will generate a list of two-element tuples.

Francisco
  • 10,918
  • 6
  • 34
  • 45
0

Another way using numpy array

import numpy as np
x= [ 'light', 'double' , 'motor' , 'boolean' , 'brakes' , 'unit 8']
a = np.array(x).reshape(3,2)
# print(a) will print as numpy array
# for printing as a list
print(a.tolist())
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29