Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate multiple lists with one function

Tags:

python

list

I'm doing a small project for school, where I have to compare quicksort with the built-in sort function of python. I'm actually already stuck in the beginning where I have to fill multiple lists with numbers.

def genlist():
    x = []
    while len(x) < 100
        y = randint(1,9999)
        x += [y]
    return x

this is my code. This code works, but it only creates one list. However my goal is to create multiple lists with different numbers of length.

Tried something like that:

def genlist():
    x,y,z = []
    while len(x,y,z) < 100, 1000, 10
        y = randint(1,9999)
        x += [y]
    return x

but doesn't work obviously D:

like image 451
epYa Avatar asked Jan 21 '16 18:01

epYa


People also ask

How do you make multiple lists at once in Python?

You can simply use a for loop to create n lists. The variable a_i changes as i increments, so it becomes a_1, a_2, a_3 ... and so on.

How do I add multiple lists to one list?

Using + operator to append multiple lists at once This can be easily done using the plus operator as it does the element addition at the back of the list.

How do you print multiple lists in Python?

Printing a list in python can be done is following ways: Using for loop : Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it.


3 Answers

I think that this is what you want instead, a function that can generate random lists of various lengths:

import random


def main():
    print(generate_list(100))
    print(generate_list(1000))
    print(generate_list(10))


def generate_list(length):
    return [random.randrange(10000) for _ in range(length)]

if __name__ == '__main__':
    main()

Call generate_list with the length of the list you want created and use the returned result.

like image 111
Noctis Skytower Avatar answered Nov 14 '22 23:11

Noctis Skytower


You're close - you can make just a couple of changes to your existing code to get what you may want:

def genlist():
    x = []
    limit = randint(50, 200)
    while len(x) < limit:
        y = randint(1,9999)
        x.append(y)  # Use the .append() function is more common
    return x

That will give you a list of random numbers that have a random length between 50 and 200.

As Noctis pointed out, though, there are some features in Python that can reduce the amount of code you have to write. And when you become more familiar, it's easier to read, too.

The first thing we can do is change that while loop into a for loop. Whenever you know before hand how many of a thing you want, for loops are the way to go.

def genlist():
    x = []
    for _ in range(randint(50, 200)):  # use xrange if you're on Python2
        y = randint(1,9999)
        x.append(y)  # Use the .append() function is more common
    return x

The next change we can make is something called a list comprehension. This basically just takes all of that code that makes up your loop and jams it inside the []. Here is a great visual guide to list comps

def genlist():
    x = [randint(1, 9999) for _ in range(randint(50, 200))]
    return x

And you can trim that down by just returning the list comprehension instead of assigning it to a varaible:

def genlist():
    return [randint(1, 9999) for _ in range(randint(50, 200))]

And if you so desire, you can parametrize your function so the caller can customize the outputs - and to make things even better you can give them default parameters so they get the current behavior by default unless they want to override it:

def genlist(randmin=1, randmax=9999, minsize=50, maxsize=200):
    return [randint(randmin, randmax) for _ in range(randint(minsize, maxsize)]
like image 39
Wayne Werner Avatar answered Nov 14 '22 23:11

Wayne Werner


I know this is an old question, but think I have a better answer.

The answer by Noctis Skytower requires additional print lines in the __main__ function to get the required number of lists. In my solution the number of lists required is an argument to the function.

I came across this because today I had the exact same question. I have only been learning Python for a short time, but I have hacked out a solution.

The user can give number and length as parameters. The output is a list of lists.

import random


def create_multiple_lists(number, length):
    lists_master = []
    for i in range(number):
        sub_list = []
        for j in range(length):
            sub_list.append(random.randint(0, 1000))
        lists_master.append(sub_list)
    print(lists_master)


create_multiple_lists(20, 10)
like image 41
Brett McGregor Avatar answered Nov 14 '22 23:11

Brett McGregor