Reputation: 495
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
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
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