-1

I know the title is a bit confusing but I couldn't think of a better way of describing it. This is python code.

Say I have a list a=["blue","yellow", "red", "green"]. How would I split it so it looks like this: a=[["blue","yellow"],["red","green"]]?

Largecooler21
  • 147
  • 3
  • 12

1 Answers1

0

Just make appropriate slices and put them in a list of their own:

a = [a[:2], a[2:]]

If you don't know the length of the list beforehand, you can use a comprehension:

chunksize = 2
a = [a[i:i+chunksize] for i in range(0, len(a), chunksize)]
user2390182
  • 72,016
  • 6
  • 67
  • 89