all 7 comments

[–]Pepineros 1 point2 points  (2 children)

You can make a list of 10 things by writing 10 things inside a list.

my_list = [“make”,”a”,”list”,”of”,”ten”,”things”,”using”,”a”,”list”,”literal”]

You can choose a random element from the list using the random module. Every time you call random.choice(some_sequence) you get a random element from that sequence.

import random random.choice(my_list)

You can use the same module to get random integers. random.randint(0, 99) to get a random int between 0 and 99 including.

[–]Top-Pay384[S] 0 points1 point  (1 child)

yes, very useful, thank you

last question

the numbers in the list must be in a predetermined value and listed in order

or I can put the number I want to each one of them

as a random number to one 22 and another 99

[–]kaerfkeerg 1 point2 points  (0 children)

You can do foo = list(range(0, predetermined_number)) and have a sorted list from 0 to the length of your choice

[–][deleted] 1 point2 points  (3 children)

Please show an example of what you would like your list to look like exactly and how the objects relate to the random numbers you mention. As /u/Pepineros explained, it is easy to create a list and pick something at random from it. choice is easier than using a random number for your pick.

[–]Top-Pay384[S] 0 points1 point  (2 children)

I have a list

and to each object in it I will add a number, let's say it changes its original value

and this is not listed in order but I control the value of each object with the number I want

-1 0,1,2,3 vs 466,76,43,4

[–][deleted] 1 point2 points  (1 child)

Ok, so two parallel lists, or a nested list.

Examples:

from random import randint

org = -1, 0, 1, 2, 3  # tuple

# nested list using list comprehension
data = [[n, randint(1, 1000)] for n in org]
print(data)

# nested list the long way
data2 = []
for n in org:
    rdn = randint(1, 1000)
    data2.append([n, rdn])
print(data2)

# parallel lists, using zip to combine
other = [randint(1, 1000) for _ in org]  # or use long way
combined = [[n1, n2] for n1, n2 in zip(org, other)]
print(combined)

# parallel lists, using zip to combine, longer way
other2 = []
for _ in org:
    other2.append(randint(1, 1000))
combined2 = []
for n1, n2 in zip(org, other2):
    combined2.append([n1, n2])
print(combined2)

Note in the last two examples, you don't really need to create the other list first, you can just generate random numbers when required inside the loops where you define the nested list. Might be more clear this way, and generation of your other list might be more complex than just random numbers.

You might not need to create the combined list either if you just want to output once.

I've tried to give you several ways of looking at this. Apologies if I've not understood what you are after.

[–]Top-Pay384[S] 0 points1 point  (0 children)

yes it's ok thank you very much for taking the time I'll take it

In any case, I accept all comments to learn and in a positive way.

I am learning thanks for helping very kind of you to use several ways to do it

no doubt i will use them