Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create list by repeated application of function

Tags:

python

list

I want this:

[foo() for _ in xrange (100)]

but beautifuller. ?

like image 988
Manav Avatar asked Jul 13 '11 18:07

Manav


People also ask

How do I make a list repeated values in Python?

We can use the append() method, the extend() method, or the * operator to repeat elements of a list in python.

How do you apply a function to every item in a list?

Use the map() Function to Apply a Function to a List in Python. The map() function is used to apply a function to all elements of a specific iterable object like a list, tuple, and more. It returns a map type object which can be converted to a list afterward using the list() function.


6 Answers

Your list comprehension is already beatiful and effective but if you need several options to do the same things then i think you can use map here. In case you need to call a certain function the specified number of times use:

# in case your func looks like
def func():
    # do something
#then
map(func(), xrange(numberOfTimes))

In case your function need value from range then you can use map with lambda:

# in case your func looks like
def func(value):
    # do something with value
#then
map(lambda val: func(val), xrange(numberOfTimes))

Or in case you need to use data from several lists of the same length:

# in case your func looks like
def func(value1, value2):
    # do something with values
#then
map(lambda val: func(*val), zip(xrange(10), xrange(10,20)))

And so on...

like image 125
Artsiom Rudzenka Avatar answered Nov 15 '22 17:11

Artsiom Rudzenka


You can write a generator repeat like this:

def repeat(times, func, *args, **kwargs):
    for _ in xrange(times):
        yield func(*args, **kwargs)

Then:

list(repeat(100, foo))

It also accepts arguments to be passed on to the function, so you can:

from random import randint
list(repeat(100, randint, 1, 100))   # 100 random ints between 1 and 100

Since it's a generator, you can pipe it into any kind of iterable, be it a list (as here) or a tuple or a set, or use it in a comprehension or a loop.

like image 31
kindall Avatar answered Nov 15 '22 16:11

kindall


I'm afraid you're not gonna get it any prettier than that in Python, except that some people would advise against _ for an "anonymous" variable. This is the Pythonic idiom for doing what you want.

(The _ can be considered confusing to novices because it can be mistaken for special syntax. I use it, but only in the "expert parts" of my code. I also encounter it more and more often, but opinion still seems a bit divided on this one.)

like image 31
Fred Foo Avatar answered Nov 15 '22 17:11

Fred Foo


Depending on your definition of "beautifuller", you may prefer this:

map(lambda x: foo(), xrange(100))

Although what you have already is much nicer IMO.

like image 41
actionshrimp Avatar answered Nov 15 '22 15:11

actionshrimp


Depending on what it does, you can make foo() a generator.

like image 22
nmichaels Avatar answered Nov 15 '22 17:11

nmichaels


In case foo() always returns the same result, you could use

[foo()]*100

This has the advantage that foo() is only called once.

Edit: As @larsmans points out this only makes sense though if foo() returns an immutable result.

In all other cases, your solution is fine!

like image 25
mhyfritz Avatar answered Nov 15 '22 16:11

mhyfritz