Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Replacing a variable in a list using a for-loop? [duplicate] in python ?

By M.K. Verma · 3 May 2022

Replacing a variable in a list using a for-loop? [duplicate] in python ?

In your exampe i is an item in list, not an index:

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

for i, val in enumerate(cards):
    if val == 11:
        cards[i] = 1


print(cards)