Reputation: 13
Suppose you have two lists:
x = [Jack, John, Suzie, Bill, James]
y = [93, 88, 100, 72, 82]
I have written a code to average the numbers in y, and remove all numbers below that average. Is there a way to also delete the corresponding names in list x as well?
So, if 88, 72, and 82 were removed from y, how can I use that to remove John, Bill, and James from list x?
Here is a snippet of my current code:
newName = nameList + listName
newGrade = gradeList + listGrade
print(newName)
print(newGrade)
avg = getAverage(newGrade)
print('The average of the list is:', avg)
gradeAvg = [i for i in newGrade if i > avg]
nameAvg =
I have isolated all elements in gradeAvg, and need to take those same ones out from nameAvg.
Upvotes: 1
Views: 56
Reputation: 31
You could also try to use dictionaries.
Try looking into this https://docs.python.org/3/tutorial/datastructures.html#dictionaries
But if you need to do with separate lists, you could compare their indexes. That is, get the index of 88 in list y and remove the value at same index from x.
Check this for finding index Finding the index of an item in a list
Upvotes: 0
Reputation: 9
To do that just keep the index of the deleted value in y and then remove the value at the same index in x. Like this:
x = ['Bill' , 'Bob', 'Jane', 'Karen']
y = [12, 24, 19, 45]
for element in enumerate(y):
if element[1] < your_value:
del y[element[0]]
del x[element[0]]
Upvotes: 0
Reputation: 98941
You an use:
x = [v for k,v in zip(y,x) if k > 90] # 90 or whatever number you want.
# ['Jack', 'Suzie']
Upvotes: 1