Adding to an element in a list of elements attached to a key in a dictionary (Python)
Clash Royale CLAN TAG#URR8PPP
Adding to an element in a list of elements attached to a key in a dictionary (Python)
players = "Mark": 0, "Bob": 0
print(players["Mark"])
players["Mark"] += 1
print(players["Mark"])
Gives output
0
1
Now I want to use this
players = "Mark": [1, 2, 3], "Bob": [1, 2, 3]
I only want to add 1 to the first element in the list attached to the key "Mark". Then, I want to print that first element.
How would I do this?
1 Answer
1
Like so:
players = "Mark": [0,1,2], "Bob": [0,1,2]
print(players["Mark"])
players["Mark"][0] += 1
print(players["Mark"][0])
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
There isn't any other possible answer to this I guess :) As simple as that
– Ankit Gupta
42 secs ago