Reputation: 43
I want to generate a Tuple of the below format.
Tuple = (2,20,200,2000)
Its basically adding 0's after a number. How do i generate these format tuple in python 3.
Upvotes: 1
Views: 53
Reputation: 2939
You could do something like this:
seed = 2
tuple(seed * 10**n for n in range(4))
Given a seed it will create a list of seed * 1
, seed * 10
, seed * 100
, etc. and then cast that as a tuple
Upvotes: 3
Reputation: 50759
You could use tail recursion (although tail recursion within python doesn't add much of a benefit):
tuple = (2, )
n = 4
def generate(n, tuple):
if n == 1:
return tuple
else:
tuple = tuple + (tuple[-1]*10, )
return generate(n-1, tuple)
print(generate(n, tuple))
Output:
>> (2, 20, 200, 2000)
Upvotes: 0
Reputation: 947
Something like this would work:
tuple = ()
n = 5 # amount of items
num = 2 # number you want to have in the tuple
for x in range(n):
new_item = num*10**x
tuple += (new_item,)
print(tuple)
Upvotes: 0