0

I have the following list:

x =['Adam', 'G', '24', '1983', 'Aug', 'August Birthday', 'Steve', 'F', '31', '1970', 'Sep', 'sept bday']

I would like to get the above list into another list but in a way I can work with it like this

x = [('Adam', 'G', '24', '1983', 'Aug', 'August Birthday'),('Steve', 'F', '31', '1970', 'Sep', 'sept bday')]

The Pattern is x = [(0,1,2,3,4,5),(0,1,2,3,4,5)] etc....

What is a good way to do this?

I have tried to iterate over the list using a count and after each line adding 1 to the count so I can get to 6 then start count over again, but I am not sure how to get it into the desired list.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Trying_hard
  • 8,931
  • 29
  • 62
  • 85
  • It may also be worth looking at how that first list is generated in the first place and changing that unless you also need it in the long format. – kylieCatt Jan 08 '14 at 20:42

1 Answers1

3
size_of_new = 5
print zip(*[iter(x)]*size_of_new)

is my favorite way of doing this ... however

[x[i:i+size_of_new] for i in range(0,len(x),size_of_new)]

is maybe more readable

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • I know what the asterisk means in the first example, but I am surprised by the fact that you don't put a comma between *[iter(x)] and *size_of_new. Why is this? – Totem Jan 08 '14 at 20:46
  • 1
    because its really more like `zip(*[1]*3)` => `zip(*[1,1,1])` ,whereas `zip(*[1],*3)` would result in an error – Joran Beasley Jan 08 '14 at 20:50
  • Your first example truncates `len(x) % size_of_new` elements at the end of list `x`. – Air Jan 08 '14 at 21:06
  • Yeah... But that shouldn't matter given the problem statement – Joran Beasley Jan 08 '14 at 21:50