Home > front end >  How can I reach values of a key pair value within a value with list comprehensions?
How can I reach values of a key pair value within a value with list comprehensions?

Time:02-01

I want to loop through "prices" dict (Imean t1 or t2, not e1 or e2) and get their values by using list comprehensions

def combine2(ticks: Mapping[str, Generator[Tick, None, None]]) -> None:
    [????]

t1 = Tick(time=datetime.now(), prices={'i1': 11.11, 'i2': 12.12, })
t2 = Tick(time=datetime.now(), prices={'i1': 13.13, 'i2': 14.14, })
t3 = Tick(time=datetime.now(), prices={'i1': 15.15, 'i2': 16.16, })

combine2({'e1': t1, 'e2': t2, 'e3': t3})

CodePudding user response:

Thanks for your question, Try this:

def combine2(ticks: Mapping[str, Tick]) -> None:
    prices = [price for tick in ticks.values() for price in tick.prices.values()]
    print(prices)
  •  Tags:  
  • Related