0

I'm getting a list such as

StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']

I am printing it like:

for each in StudentStatus:
    print each

Hence the output is

Physics Pass
Chem Distinction
Maths Average

I want to print it the other way. I tried -

for each in StudentStatus:
    print each[0], each[1]

which just printed

Physics
Chem
Maths

How to split the list to get proper output?

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
NikAsawadekar
  • 57
  • 1
  • 5

2 Answers2

0

Assuming the last word is the grade, then you can use str.rpartition:

StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']

for item in StudentStatus:
    print 'Subject: {0} - Grade: {2}'.format(*item.rpartition(' '))

Output:

Subject: Physics - Grade: Pass
Subject: Chem - Grade: Distinction
Subject: Maths - Grade: Average
Jon Clements
  • 132,101
  • 31
  • 237
  • 267
0
StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']
for echo in StudentStatus:
    first, second = echo.split()
    print first, second
Nir Alfasi
  • 51,812
  • 11
  • 81
  • 120