0

How to make list of lists from:

data = ['a','b','c','d','e','f','g','h','i']

to

data = [['a','b','c'],['d','e','f'],['g','h','i']]
Terence Ng
  • 442
  • 2
  • 8
  • 19
  • I am sorry that I am newbie in python. I fail to assign value of data by using data = [data[i, i+3] for i in range(0, len(data), 3)] in order to achieve the result of [['a','b','c'],['d','e','f'],['g','h','i']] – Terence Ng Jun 08 '13 at 09:47
  • you've made a typo: `[data[i: i+3] for i in range(0, len(data), 3)]` – jamylak Jun 08 '13 at 09:51
  • you should just copy paste the code to see if it works first – jamylak Jun 08 '13 at 09:52

1 Answers1

1
[a[i:i+3] for i in range(0, len(a), 3)]
jamylak
  • 128,818
  • 30
  • 231
  • 230
tom
  • 21,844
  • 6
  • 43
  • 36