CJ12
CJ12

Reputation: 495

Removing an element from a list and a corresponding value

myList = ['a', 'b', 'c']
myOtherList = [1, 2, 3]

If I chose one element from myList, how do I remove the corresponding value from myOtherList. I am choosing elements from myList at random and need to make sure the corresponding value from myOtherList is removed.

Upvotes: 0

Views: 51

Answers (2)

ccfima
ccfima

Reputation: 23

Pick the item, then zip the lists so that they are corresponding, for example

listA = [1,2,3,4]
listB = [a,b,c,d]

joinedlist = zip(listA,listB)

indexpick = randomindexpicker()  # lets assume that this is '2'

del joinedlist[indexpick]

listA, listB = zip(*joinedlist)

This should give you

listA = [1,2,4]
listB = [a,b,d]

Upvotes: 0

axblount
axblount

Reputation: 2662

It would be easier to pick an index at random from myList.

from random import randint

myList = ['a', 'b', 'c']
myOtherList = [1, 2, 3]

index = randint(0, len(myList)-1)

del myList[index]
del myOtherList[index]

But if you are stuck with picking an item, just get the index of the item with the... index function!

index = myList.index(chosen_element)

Upvotes: 2

Related Questions