Reputation: 105
I have a tuple like:
mytuple = (10,10)
mylist = []
mylist.append(mytuple)
I would like to subtract 1 from each iteration of x in mytuple until there is only 0 so that mylist would look like
[(10,10),(9,10),(8,10),(7,10),etc.,(0,10)]
I was thinking about a for loop, but I couldn't really figure out how to get this output. Thanks.
Upvotes: 1
Views: 171
Reputation: 89
Try something like:
mytuple = (10,10)
mylist = [(i,mytuple[1]) for i in range(mytuple[0],-1,-1)]
Upvotes: 1
Reputation: 15934
range lets you specify the step taken with your iteration, which lets you do something like this:
mytuple = (10,10)
mylist = []
for i in range(mytuple[0], -1, -1):
mylist.append((i, mytuple[1]))
print(mylist)
See this in action here: http://ideone.com/wnM5zb
Upvotes: 0
Reputation: 4580
mytuple = (10,10)
mylist = []
mylist.append(mytuple)
# store mytuple's first value
val = mytuple[0]
# while val is greater than 0
while (val > 0):
# subtract 1 from val
val -= 1
# generate tuple and add to list
mylist.append((val,10))
print(mylist)
Upvotes: 1