1

i have a list like:

list=['2,130.00','2,140.00','2,150.00','2,160.00']

i would like to use a function like

def f(iterable):
    yield from iterable

and applying

float(item.replace(',','')) for item in iterable

at the same time so that

f(list)

returns

[2130.00,2140.00,2150.00,2160.00]

I know

[float(x.replace(',','')) for x in list]

works here but it is to understand how to use yield from in a function and modifying items in the iterable at the same time. Maybe i have to use *args and/or **kwargs in the function but not sure i have and how to.

1
  • What isn't working here? Commented Sep 22, 2014 at 10:31

1 Answer 1

3

yield from is a pass-through; you are delegating to the iterable. Either wrap the iterable in a generator expression to transform elements, or don't use yield from but an explicit loop:

def f(iterable):
    yield from (i.replace(',', '') for i in iterable)

or

def f(iterable):
    for item in iterable:
        yield item.replace(',', '')
Sign up to request clarification or add additional context in comments.

4 Comments

thanks so in the end yield from is not very useful in this type of context
@addequttorr: yield from is extremely useful when delegating to other generators. It is not the right tool for transforming the data from another iterable.
Are there any post documenting this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.