unpacking vars from list of tuples
rossgk at gmail.com
Wed Sep 16 00:31:31 CEST 2009
On Sep 15, 6:00 pm, Andre Engels <andreeng... at gmail.com> wrote:
> On Tue, Sep 15, 2009 at 11:51 PM, Ross <ros... at gmail.com> wrote:
> > I'm inexperienced with some of the fancy list slicing syntaxes where
> > python shines.
> > If I have a list of tuples:
> > k=[("a", "bob", "c"), ("p", "joe", "d"), ("x", "mary", "z")]
> > and I want to pull the middle element out of each tuple to make a new
> > list:
> > myList = ["bob", "joe", "mary"]
> > is there some compact way to do that? I can imagine the obvious one
> > of
> > myList = 
Thanks both Chris and André. That's quite obvious once it's pointed
out for me. Thanks especially for the terminology that will make
learning the related concepts a bit easier.
> > for a in k:
> > myList.append(a)
> > But I'm guessing Python has something that will do that in one line...
> > Any suggestion is appreciated...
> You can use a list comprehension:
> myList = [a for a in k]
> André Engels, andreeng... at gmail.com
More information about the Python-list