1

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.

Ethan
  • 13
  • 4
  • 3
    If the values in the two lists are related, why not use a list of tuples, and manipulate that single list? – Jonathon Reinhart May 01 '20 at 00:05
  • You might want to share that code you mention. This new implementation you want might fit in that code. – revliscano May 01 '20 at 00:05
  • @JonathonReinhart Unfortunately, the guidelines our professor gave us specifically said we had to use lists throughout the entire program! – Ethan May 01 '20 at 00:51
  • 1
    @revliscano Thank you for the advice! I included it and will keep that in mind for the future! – Ethan May 01 '20 at 00:51

3 Answers3

1

You an use:

x = [v for k,v in zip(y,x) if k > 90] # 90 or whatever number you want.
# ['Jack', 'Suzie']

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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]]
twingoof
  • 9
  • 1
  • 4
  • This works great, however, is there a way I can do it for all indexes it applies to? When I try to use the entire list as a range, I get an error. – Ethan May 01 '20 at 00:37
  • @Ethan what do you mean by using the entire list as range ? The for loop already look every index in the list. – twingoof May 01 '20 at 13:12
0

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

Demnofocus
  • 21
  • 6
  • Unfortunately, our professor specifically said we weren't allowed to use dictionaries! Thank you for the link about indexes, though! – Ethan May 01 '20 at 00:46