Home > front end >  selecting certain elements in a list - python
selecting certain elements in a list - python

Time:11-16

I am looking to edit elements in a list bar one element - I am wondering how to do so? I am making one element go up, the one selected, and the others to go down. I have worked out how to do the first bit, just not the second. Here is my code so far

score = [70,60,80]
action = ["one person off", 2, 200]

def take_action(score, action):
    x = action[1]
    if action[0] = "one person off":
        score[x]  = 30

I am looking for 80 to go to 110 and the others to go down by 10 each.

Any help would be greatly appreciated!

CodePudding user response:

You could implement the action as loop:

for i,s in enumerate(score):
    score[i] = s 30 if i == x else s-10

CodePudding user response:

You can use a list comprehension:

score = [value 30 if i==x else value-10 for i,value in enumerate(score)]

This will give you back a new list, with the xth element increased by 30 and the others decreased by 10.

  • Related