-1

Having troubles breaking up and regrouping my list so that information about a single airline company and it's fees are in one list element, by a loop, with every 6 elements becoming an element on its own.

myList = ['Southwest', '0', '0', '0', '0', 'Yes!', 'JetBlue', '20', '35', '75', '125', 'Yikes']

(This list goes on for 6 airlines in total, there's 2 listed here, the list is in order of Airline name, fees(4), and then a short word describing the value)

I need to group Southwest, 0, 0, 0, 0, Yes! together as an element.

Then group JetBlue, 20, 35, 75, 125, Yikes together as an element.

Then I will do this for 4 more airlines. I obviously want to use a loop since it's multiple airlines.

Blake
  • 21
  • 3
  • Length of a segement is 5 so: `[myList[i:i+5] for i in range(0, len(myList), 5)]` would do it. – What May 02 '18 at 00:52

1 Answers1

0

This works for every number of elements inside myList

def chunks(myList):
    return [ ', '.join(myList[i:i+6]) for i in range(0, len(myList)-1, 6) ]

myList = ['Southwest', '0', '0', '0', '0', 'Yes!', 'JetBlue', '20', '35', '75', '125', 'Yikes']
print(chunks(myList))
Daniele Cappuccio
  • 1,952
  • 2
  • 16
  • 31