Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list of strings by joining elements of another list [duplicate]

Tags:

python

arrays

Possible Duplicate:
How do you split a list into evenly sized chunks in Python?
Merge two lists in python?

Original data in array:

a = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

Desired output:

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

I know using the while statement is inefficient, so I need help in this.

like image 838
Natsume Avatar asked Sep 06 '12 06:09

Natsume


1 Answers

[' '.join(a[i:i+3]) for i in range(0, len(a), 3)]
like image 146
Aesthete Avatar answered Oct 12 '22 23:10

Aesthete