0

so lets say this is my list:

my_list = ['a','b','c','d','e','f']

and I want it to become:

new_list = [['a','b','c'],['d','e','f']]

I know there's a way to do it with:

new_list = []
new_list1 = []
new_list2 = []    
for x in my_list:
  new_list1.append((x[0]+x[1]+x[2]))
  new_list2.append((x[3]+x[4]+x[5]))
  new_list.append((new_list1+new_list2))
print(new_list)

is there another way to do it? Thank you

1 Answers1

3
>>> [my_list[i:i+3] for i in xrange(0, len(my_list), 3)]
[['a', 'b', 'c'], ['d', 'e', 'f']]
alko
  • 46,136
  • 12
  • 94
  • 102