justintime
justintime

Reputation: 101

Python Array to multidimensional Array

I have a problem to combining array to multidimensional array.

My Array

['Fiat', 'black', 'new', 'BMW', 'white', 'new']

The result should look like this

[['Fiat'], ['black'], ['new'], ['BMW'], ['white'], ['new']]

What do I have to do to achieve this result?

I am newbie. Please help.

Upvotes: 1

Views: 59

Answers (2)

scan
scan

Reputation: 107

Maybe you can try that:

a = ['Fiat', 'black', 'new', 'BMW', 'white', 'new']
b = []
for i in a:
    b.append([i])
print(b) # u will see result..

Upvotes: 1

tuxtimo
tuxtimo

Reputation: 2790

That simplest way is probably to use a list comprehension:

>>> l = ['Fiat', 'black', 'new', 'BMW', 'white', 'new']
>>> [[x] for x in l]
[['Fiat'], ['black'], ['new'], ['BMW'], ['white'], ['new']]

Upvotes: 6

Related Questions