Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to divide each element in a list by every element of that list

I am new to Python. I have been stuck on a rather simple problem.
I have a list
r = [2,3,4,5,6,7,8,9,10]
I want to divide each element of the list by every other element

I want the output to be 2/2, 2/3, 2/4, 2/5, 2/6, 2/7, 2/8, 2/9, 2/10, 3/2, 3/3 and so on

I have tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in r:
    print(i/i)

But the output I get is

1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0

What should I do? Thanks in advance!

>Solution :

You could use list comprehension to archive this:

r = [2,3,4,5,6,7,8,9,10]
[print(x / y) for x in r for y in r]

Which is somehow (it is not exactly) the same as:

for x in r:
  for y in r:
    print(x / y)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading