0
how do I increment an integer value to an element inside a list in a list
10 Respuestas
+ 5
I'm not sure which one suits your requirement, but I could think of two ways to achieve the same output, first, we increment by one the second element in each sub list for <n> times; or we could increment the second element by <n> just one time. I'll leave the choice to you, and possibly there are other ways, but this is as far I know : )
list = [[1,2],[3,4],[5,6],[7,8],[9,10]]
n = 10
print("Original", list, sep = "\n")
for i in range(n): # from 0 .. 9
for element in list:
element[1] += 1
print("Incremented {n} times", list, sep = "\n")
list = [[1,2],[3,4],[5,6],[7,8],[9,10]]
print("Original", list, sep = "\n")
for element in list:
element[1] += n
print("Incremented by {n} 1 time", list, sep = "\n")
+ 5
I see in your code you have this list:
list = [[1,2],[3,4],[5,6],[7,8],[9,10]]
Then you want to increase by one the value of second element of each sub list (2, 4, 6, 8, 10). Then after the first increment maybe the list looks like this:
list = [[1,3],[3,5],[5,7],[7,9],[9,11]]
Is that what you want to do? and what is this "certain condition" you want to check? can you elaborate more?
+ 5
You're welcome bro! and what game are you making?
+ 3
With a for loop you iterate through the primary elements of a list, and that are just lists in this case.
for i in range(len(your_list)):
if not i%2:
your_list[i].append.(whatever)
+ 3
Thanks that was helpful, but i meant to keep incrementing all 2nd element of the inner list (say by 1) simultaneously until a certain condition is met.
sry for the mistake question earlier . I guess it's fixed now
+ 3
yes your right. the condition is I want to keep on incrementing by 1 as long as say n<=10 means I want to keep incrementing by 1 for 10 times.
Thank you
+ 3
I'm not sure about the real name but I call it dodge. u dodge blocks falling from above. that list is the coordinate of falling blocks it changes in y coordinate. actually that certain condition is keeping incrementing untill your off screen. I'll try to figure out more
+ 3
Okay, well best of luck with the game bro ... 👍
+ 3
Thanks again👍
+ 2
Thanks a lot bro it makes sense now, i hope this works in my game