15

I want to divide a tuple by an integer. I am expecting something like this:

tuple = (10,20,30,50,80)
output = tuple/10
print(output)
output = (1,2,3,5,8)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pedram.py
  • 219
  • 1
  • 2
  • 5

2 Answers2

20

Basically the same as this answer by Truppo.

>>> t = (10,20,30)
>>> t2 = tuple(ti/2 for ti in t)
>>> t2
(5, 10, 15)
Ben Lindsay
  • 1,686
  • 4
  • 23
  • 44
9

Maybe you could try if it is a numbers tuple:

numberstuple = (5,1,7,9,6,3)
divisor = 2.0
divisornodecimals = 2

value = map(lambda x: x/divisor, numberstuple)
>>>[2.5, 0.5, 3.5, 4.5, 3.0, 1.5]
valuewithout_decimals = map(lambda x: x/divisornodecimals, numberstuple)
>>>[2, 0, 3, 4, 3, 1]

or

value = [x/divisor for x in numberstuple]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milor123
  • 537
  • 4
  • 20
  • very thanks sir it has been worked! – pedram.py Apr 03 '16 at 14:41
  • I'm happy to help, I recommend you learn about List Comprehensions – Milor123 Apr 03 '16 at 14:44
  • 1
    The answer [here](http://stackoverflow.com/a/1782003/2680824) by Truppo (and shown below) is much easier to work with and remember – Ben Lindsay Apr 03 '16 at 14:45
  • 1
    Not sure how this is the accepted answer, when they asked for the output to be tuple. The output here is list. The answer by @BenLindsay posted [here](https://stackoverflow.com/a/36386830/6260862) is the same but with the correct output type, so it should be the accepted answer. – Sindre Bakke Øyen Jul 20 '23 at 06:55