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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

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(',', '')
Pavel Šimerda
  • 5,783
  • 1
  • 31
  • 31
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343