'Convert letter grades to grade points via python 3 [closed]

I have a question for transforming a list Given: [A,B,C,D,F,W] but any letter counts, except W. Where A = 4 , B = 3, C = 2, D = 1 and F = 0 so the final output should be [4, 3, 2, 1, 0] can anyone help on this? How to print out the list of [4,3,2,1,0] right after I filter out the letter 'W'? thanks!



Solution 1:[1]

This is a cheap way of doing that. The position in the string determines the grade value.

values = "FDCBA"
grades = ['A','B','C','D','F','W']
newlist = [values.index(g) for g in grades if g in values]
print(newlist)

Solution 2:[2]

A dictionary is a way to associate key:value.
As we create a new list we simply do not add the W which is not in the dictionary. We could also choose to add it to the dictionary and assign it a value.

dic = {'A':4,'B':3,'C':2,'D':1,'F':0}
myList=['A','B','C','D','F','W']
newList=[]

print(myList)

for l in myList:
    if l in dic:
        print( dic[l], " added")
        newList.append(dic[l])
    else:
        print(l," not found")

print(newList)

Output

['A', 'B', 'C', 'D', 'F', 'W']
4  added
3  added
2  added
1  added
0  added
W  not found
[4, 3, 2, 1, 0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts
Solution 2