Nathan Miller
Nathan Miller

Reputation: 33

How to get entire list from just one value in a dictionary key?

I have this dictionary and key

y_dict[key] = [l_y, u_y, mn, price_min]

When I enter

print(y_dict)

I get this:

{4: [4.982755559285482, 6.341688893636068, 5.662222226460774, 5.369999885559082], 22: [5.847999992370606, 7.442909081198954, 6.645454536784779, 6.230000019073486], 60: [4.826400032043457, 6.142690949873491, 5.484545490958474, 5.019999980926514]}

I only want to get the price_min which lists these numbers 5.3699 , 6.2300 and 5.0199 so what I want to print is this:

5.369999885559082, 6.230000019073486, 5.019999980926514

When I enter

print(y_dict[price_min])

I only get the last price_min item in the list 5.019999980926514. How do I get the entire price_min lists instead of just the last item?

Upvotes: 0

Views: 93

Answers (2)

Barmar
Barmar

Reputation: 782130

Use a list comprehension.

print([item[3] for item in y_dict.values()])

BTW, I recommend you store the values as dictionaries, rather than hard-coding list indexes like that.

y_dict[key] = {'l_y': l_y, 'u_y': u_y, 'mn': mn, 'price_min': price_min}

The list comprehension then becomes:

print(*[item['price_min'] for item in y_dict.values()], sep=", ")

Upvotes: 1

JoshDaBosh
JoshDaBosh

Reputation: 436

You'd probably just do something like

minimums = [val[3] for key, val in y_dict.items()]

It'll be stored as an list in the variable minimums.

Upvotes: 5

Related Questions