how do I increment an integer value to an element inside a list in a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how do I increment an integer value to an element inside a list in a list

https://code.sololearn.com/cBWqH4z92DkB/?ref=app

11th Feb 2019, 6:22 PM
Viki-Sumi
Viki-Sumi - avatar
10 Answers
+ 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")
12th Feb 2019, 6:54 AM
Ipang
+ 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?
12th Feb 2019, 4:57 AM
Ipang
+ 5
You're welcome bro! and what game are you making?
12th Feb 2019, 7:15 AM
Ipang
+ 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)
11th Feb 2019, 6:43 PM
HonFu
HonFu - avatar
+ 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
12th Feb 2019, 4:41 AM
Viki-Sumi
Viki-Sumi - avatar
+ 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
12th Feb 2019, 6:09 AM
Viki-Sumi
Viki-Sumi - avatar
+ 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
12th Feb 2019, 7:21 AM
Viki-Sumi
Viki-Sumi - avatar
+ 3
Okay, well best of luck with the game bro ... 👍
12th Feb 2019, 7:23 AM
Ipang
+ 3
Thanks again👍
12th Feb 2019, 7:24 AM
Viki-Sumi
Viki-Sumi - avatar
+ 2
Thanks a lot bro it makes sense now, i hope this works in my game
12th Feb 2019, 7:13 AM
Viki-Sumi
Viki-Sumi - avatar