-2

I have a list:

grades= ['doe john     100  90  80  90', 'miller sally  70  90  60 100  80', 'smith jakob   45  55  50  58', 'white jack    85  95  65  80  75']

I want to be able to break that list so the output would be:

['doe john     100  90  80  90']
['miller sally  70  90  60 100  80']
['smith jakob   45  55  50  58']
['white jack    85  95  65  80  75']

Additionally, I would like to split the elements in the list so it looks like:

['doe', 'john',     '100',  '90',  '80',  '90']
['miller', 'sally',  '70',  '90',  '60', '100',  '80']
['smith', 'jakob',   '45',  '55',  '50',  '58']
['white', 'jack',    '85',  '95',  '65',  '80',  '75']

I'm not really sure how to go about doing this or if this is even possible as I'm just starting to learn python. Any ideas?

bkamp27
  • 9
  • 4
  • Please show what you’ve tried so far and where you’re getting stuck. Hint, yes. It’s very possible and quite simple. Give it a try. – S3DEV Apr 29 '20 at 22:20
  • I understand you want to make a list of lists, you can move from step1 to step3 in one line of code. Try to take a look to the ```split``` function – EnriqueBet Apr 29 '20 at 22:21

2 Answers2

2
for l in grades:
    l = l.split()

OR

final = [l.split() for l in grades]

See Split string on whitespace in Python

Peter S
  • 827
  • 1
  • 8
  • 24
0

This can be done quickly with .split() in a list comprehension.

grades = ['doe john     100  90  80  90', 'miller sally  70  90  60 100  80', 'smith jakob   45  55  50  58', 'white jack    85  95  65  80  75']

grades = [grade.split() for grade in grades]
print (grades)
Charles Angus
  • 394
  • 1
  • 7