Skip to content Skip to sidebar Skip to footer

Replacing Values In A List

Basically, I'm trying to create a program that will replace the values of diceList with a random integer between 1 and 6 wherever my indexList has a 1 in it. This is what I have so

Solution 1:

Is indexList a list? If so, what does a list == 1 mean?

ifindexList== 1:
    newList[i] = random.randint(1,6)

Did you perhaps mean i == 1 ?

Edit:

I think you're looking for something like this:

defreplaceValues(diceList, indexList):
    newList = diceList
    iflen(diceList) == len(indexList):
        for pos inrange(0, len(indexList)):
            if indexList[pos] == 1:
                newList[pos] = random.randint(0,6)
        return newList

Post a Comment for "Replacing Values In A List"