Joro
Joro

Reputation: 11

Python lists and iterables

in Python 3.2 How do I alter items in a list with a function and return the result of the function over the item and the item itself before altering? 10x

def some(func,seq):
    # What to do here?

Upvotes: 1

Views: 539

Answers (1)

Alexander Gessler
Alexander Gessler

Reputation: 46607

Something like

def func(my_func,seq):
    return seq, [my_func(n) for n in seq]

or

def func(my_func,seq):
    return [(my_func(n),n) for n in seq]

... your description is not very clear in that regard.

What's being used here is a list comprehension. my_func is called once for every element in seq and should return the function value for this element.

Upvotes: 3

Related Questions