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
  • 443
  • 2
  • 7
  • 18
  • 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
  • 120,885
  • 29
  • 225
  • 225
tom
  • 19,901
  • 6
  • 40
  • 36