[program-l] Re: python, Getting X number of a certain value from a list of dictionaries?

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Thu, 4 Oct 2012 18:14:02 -0700

Hi Al,

This is not a slicing, but did you try a list comprehension?

If your list of dicts is named 'day' try the following:

highList = [d['high'] for d in day[:21]]

If you enclose the right side in parens instead of brackets, you will get a 
generator instead of a list.

Richard

----- Original Message ----- 
From: "Al Puzzuoli" <alpuzz@xxxxxxxxx>
To: "Program L" <program-l@xxxxxxxxxxxxx>
Sent: Thursday, October 04, 2012 5:36 PM
Subject: [program-l] python, Getting X number of a certain value from a list 
of dictionaries?


HI everyone,
This seems like it should be really simple, but if there's a way to do
this with a single statement, the syntax is eluding me. Basically, I
have a list of dictionaries called day. So I can reference things like:
"day[0] ['High']", or "day[1] ['Date]". But, if I  want to extract a
list of 20 of a particular item, say the closing price over the last 20
days, what's the quickest way to do that? I know I could use some sort
of iterative loop, but aren't there any list slicing commands that would
work? For example, I know I can get the first 20 dictionaries by doing
something like "l=day[:21]", so can't I be more specific, "l=day[:21]
['Close']? If so, I can't for the life of me figure out the syntax.
Thanks for any help on this one,

Al

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq 

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: