Hello, I am not sure if anyone will understand what I am asking for, but here goes anyway: I am looking for a way to generate a list of tuples with random values; something like the range function except I want to have a list of tuples rather than integers. I would also like it to generate the item values randomly since I will be using this for a unit test so I want the values to be quite random.

Hope you guys understand what I'm looking for and thanks in advance.

Maybe something like this:

import random

low = 1
high = 9

mylist = [(random.randint(low, high), ) for k in range(10)]

print(mylist)

"""
a possible display of one element tuples -->
[(9,), (5,), (6,), (2,), (8,), (3,), (4,), (2,), (6,), (1,)]
"""

And extending the above to a two element tuple:

>>> import random
>>> random.seed()
>>> mylist = [ ( random.randint(0, 100), random.randint(0, 100) ) for k in range(10) ]
>>> print(mylist)
[(83, 10), (74, 51), (19, 83), (14, 36), (12, 32), (91, 27), (96, 64), (20, 38), (37, 78), (16, 36)]

Hey jlm699, how come you called random.seed()?

Expanding even further:

import random

mini = 1  # min. number of tuple indices
maxi = 5  # max. number of tuple indices
low = 1  # low-end of range of number
high = 9  # high-end of range of number

result = []
for j in range(10):
    current = []
    # add a random number of indices to the tuple
    for k in range(random.randint(mini, maxi)):
        # add another number to the current list
        current.append(random.randint(low, high))
    # convert current list into a tuple and add to resulting list
    result.append(tuple(current))
"""
my result:
[(2, 1), (5, 9, 6, 1), (3,), (7, 1, 3), (5, 2, 5, 6, 1), (3,), (1, 1, 9, 8, 7), (2,), (9, 5, 3), (6,)]
"""

Just allows for a random number of indices in each tuple.

Hey jlm699, how come you called random.seed()?

Habit

commented: oh I see; I thought it was some requirement that I'd been missing. +6

Thank you so much, that really helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.