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 do I add an item number to each number in the list?

Add an index number to each element of the array in it
for example
I need to output it like this test. 7; 3; 0; -5; 1; 2; 8; 4. The result. 7; 4; 2; -2; 5; 7; 14; 11.

import random
rand_mass = []
n = 10
for i in range(n):
    rand_mass.append(random.randint(-5, 9))

and what should I do next?

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

>Solution :

Use a comprehension

import random

n = 10

#if your index starts at 0
rand_mass_0 = [random.randint(-5, 9) + idx for idx in range(n)]

# if your index starts at 1
rand_mass_1 = [random.randint(-5, 9) + idx for idx in range(1, n+1)]
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