-3

I am trying to replace the value 11 with the value of 1.

What am I doing wrong?

enter image description here

I want it to be:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39

3 Answers3

0
for i in range(len(cards)):
    if cards[i]==11:
        cards[i]=1
0

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)
var211
  • 596
  • 5
  • 10
0

You could do this with a list comprehension:

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
[1 if i==11 else i for i in cards]

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
Nin17
  • 2,821
  • 2
  • 4
  • 14