0
record=['MAT', '90', '62', 'ENG', '92','88']

course='MAT'

suppose i want to get the marks for MAT or ENG what do i do? I just know how to find the index of the course which is new[4:10].index(course). Idk how to get the marks.

Bob
  • 45
  • 7

2 Answers2

1

Try this:

i = record.index('MAT')
grades = record[i+1:i+3]

In this case i is the index/position of the 'MAT' or whichever course, and grades are the items in a slice comprising the two slots after the course name.

You could also put it in a function:

def get_grades(course):
    i = record.index(course)
    return record[i+1:i+3]

Then you can just pass in the course name and get back the grades.

>>> get_grades('ENG')
['92', '88']
>>> get_grades('MAT')
['90', '62']
>>> 

Edit

If you want to get a string of the two grades together instead of a list with the individual values you can modify the function as follows:

def get_grades(course):
    i = record.index(course)
    return ' '.join("'{}'".format(g) for g in record[i+1:i+3])
elethan
  • 16,408
  • 8
  • 64
  • 87
0

You can use index function ( see this https://stackoverflow.com/a/176921/) and later get next indexes, but I think you should use a dictionary.

Community
  • 1
  • 1
Dante
  • 295
  • 2
  • 12