unpacking vars from list of tuples

R

Ross

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 = []
for a in k:
myList.append(a[1])

But I'm guessing Python has something that will do that in one line...

Any suggestion is appreciated...

Ross.
 
C

Chris Rebert

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 = []
for a in k:
  myList.append(a[1])

But I'm guessing Python has something that will do that in one line...

Indeed:

myList = [a[1] for a in k]

Google for "list comprehension python".

Cheers,
Chris
 
A

Andre Engels

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 = []
for a in k:
  myList.append(a[1])

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[1] for a in k]
 
W

Wolfram Hinderer

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"]

if a tuple is OK: zip(*k)[1]
 
R

Ross

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.

Ross

for a in k:
  myList.append(a[1])
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[1] for a in k]
 
T

Tim Chase

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 = []
for a in k:
myList.append(a[1])

But I'm guessing Python has something that will do that in one line...

To add some readability to the other suggested solutions, I'd use
tuple unpacking

my_list = [name for status, name, code in k]

Not knowing what [0] and [2] are, I randomly designated them as
"status" and "code", but you likely have your own meanings. If
you don't, you can always just use the "_" convention:

my_list = [name for _, name, _ in k]
# or
my_list = [name for (_, name, _) in k]

As an aside, "my_list" is preferred over "myList" in common
Python practice. I don't know if there's a preferred convention
for "with vs without" the parens in such a tuple-unpacking list
comprehension.

-tkc
 
R

Ross

Thanks Tim,

That's actually the stuff I was trying to remember.

my_list = [name for _, name, _ in k]

I recalled using some underscores for nice dense unnamed variable
unpacking before, but couldn't recall the process.

Thanks for that.

Ross.


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 = []
for a in k:
myList.append(a[1])
But I'm guessing Python has something that will do that in one
line...

To add some readability to the other suggested solutions, I'd use
tuple unpacking

my_list = [name for status, name, code in k]

Not knowing what [0] and [2] are, I randomly designated them as
"status" and "code", but you likely have your own meanings. If you
don't, you can always just use the "_" convention:

my_list = [name for _, name, _ in k]
# or
my_list = [name for (_, name, _) in k]

As an aside, "my_list" is preferred over "myList" in common Python
practice. I don't know if there's a preferred convention for "with
vs without" the parens in such a tuple-unpacking list comprehension.

-tkc
 
R

Ross

Cool - Now that would be some seriously dense, efficient code! Will
have to play with numpy sometime.


R.
 
R

Ross

Learning my way around list comprehension a bit. I wonder if
someone has a better way to solve this issue. I have a two element
dictionary, and I know one of the keys but not the other, and I want
to look up the other one.

So I have this dictionary:

aDict = {'a': 'bob', 'b': 'stu'}

I know that the dictionary contains two keys/value pairs, but I
don't know the values nor that the keys will be 'a' and 'b'. I
finally get one of the keys passed to me as variable BigOne. e.g.:

BigOne = "a"

The other key, call it littleOne remains unknown. It might be "b"
but could be "c", "x", etc... I later need to access both values...

I have something that works, with list comprehension - but wonder if
there's a more brief/elegant way to get there:

BigValu = aDict[BigOne]
temp = [ thing for thing in aDict if thing != BigOne ]
LittleValu = aDict[ temp[0] ]

Any thoughts?

- Ross.
 
I

Ishwor

Ross
Hi.
So I have this dictionary:

aDict = {'a': 'bob', 'b': 'stu'}
Yes.

I know that the dictionary contains two keys/value pairs,  but I don't know
the values nor that the keys will be 'a' and 'b'.   I finally get one of the
keys passed to me as variable BigOne. e.g.:

BigOne = "a"

Right. aDict[BigOne] will give you - 'bob' which may/maynot be what
you want by the looks of it, you want aDict[BigOne].
aDict.keys() ['a', 'b']
aDict.values()
['bob', 'stu']

keys() / values() return list so you do not have to worry about
explicitly getting list.
The other key, call it  littleOne remains unknown.  It might be "b" but
could be "c", "x", etc...   I later need to access both values...

I have something that works, with list comprehension - but wonder if there's
a more brief/elegant way to get there:

If you know the dictionary name in this case aDict (which you don't
mention you know or not), you can just traverse it using easily as
such: print k, aDict[k];
[[i,a] for i in aDict] [['a', 'bob'], ['b', 'stu']]
[[i,a] for i in aDict][0] ['a', 'stu']
[[i,a] for i in aDict][0][0] 'a'
[[i,a] for i in aDict][0][1]

'bob'
 
T

Tim Chase

Learning my way around list comprehension a bit. I wonder if
someone has a better way to solve this issue. I have a two element
dictionary, and I know one of the keys but not the other, and I want
to look up the other one.

Several ways occur to me. Of the various solutions I played
with, this was my favorite (requires Python2.4+ for generator
expressions):

d = {'a': 'alice', 'b':'bob'}
known = 'a'
other_key, other_value = (
(k,v)
for k,v
in d.iteritems()
if k != known
).next()

If you just want one or the other, you can simplify that a bit:

other_key = (k for k in d.iterkeys() if k != known).next()
other_key = (k for k in d if k != known).next()

or

other_value = (v for k,v in d.iteritems() if k != known).next()

If you're using pre-2.4, you might tweak the above to something like

other_key, other_value = [
(k,v)
for k,v
in d.iteritems()
if k != known
][0]
other_key = [k for k in d if k != known)[0]
other_value = [k for k in d.iteritems if k != known][0]

Hope this helps,

-tkc
 
R

Ross

Thanks Tim (and Ishwor) for the suggestions, those are structures
that somewhat new to me - looks good! I'll play with those. At
this rate I may soon almost know what I'm doing.

Rgds
Ross.


Learning my way around list comprehension a bit. I wonder if
someone has a better way to solve this issue. I have a two
element dictionary, and I know one of the keys but not the other,
and I want to look up the other one.

Several ways occur to me. Of the various solutions I played with,
this was my favorite (requires Python2.4+ for generator expressions):

d = {'a': 'alice', 'b':'bob'}
known = 'a'
other_key, other_value = (
(k,v)
for k,v
in d.iteritems()
if k != known
).next()

If you just want one or the other, you can simplify that a bit:

other_key = (k for k in d.iterkeys() if k != known).next()
other_key = (k for k in d if k != known).next()

or

other_value = (v for k,v in d.iteritems() if k != known).next()

If you're using pre-2.4, you might tweak the above to something like

other_key, other_value = [
(k,v)
for k,v
in d.iteritems()
if k != known
][0]
other_key = [k for k in d if k != known)[0]
other_value = [k for k in d.iteritems if k != known][0]

Hope this helps,

-tkc
 
V

Vlastimil Brom

2009/9/18 Ross said:
Learning my way around list comprehension a bit.   I wonder if someone has a
better way to solve this issue.  I have a two element dictionary, and I know
one of the keys but not the other, and I want to look up the other one.

So I have this dictionary:

aDict = {'a': 'bob', 'b': 'stu'}

I know that the dictionary contains two keys/value pairs,  but I don't know
the values nor that the keys will be 'a' and 'b'.   I finally get one of the
keys passed to me as variable BigOne. e.g.:

BigOne = "a"

The other key, call it  littleOne remains unknown.  It might be "b" but
could be "c", "x", etc...   I later need to access both values...

I have something that works, with list comprehension - but wonder if there's
a more brief/elegant way to get there:

BigValu = aDict[BigOne]
temp =  [ thing for thing in aDict if thing != BigOne ]
LittleValu = aDict[ temp[0] ]

Any thoughts?

- Ross.

Hi,
not a list comprehension, but another useful approach to get the
complement of (a) given item(s) might be the set operations.
(As you are working with dict keys, the requirements for set elements
are automatically fulfilled.
data_dict = {'a': 'bob', 'b': 'stu'}
not_wanted_key = "a"
other_key = (set(data_dict.iterkeys()) - set([not_wanted_key,])).pop()
other_key 'b'
data_dict[other_key] 'stu'

vbr
 
S

Sion Arrowsmith

Vlastimil Brom said:
other_key = (set(data_dict.iterkeys()) - set([not_wanted_key,])).pop()

other_key = set(data_dict.iterkeys()).difference([not_wanted]).pop()
saves you the construction of an unnecessary set instance. At the
cost of a bit more verbosity, you can get rid of a second set:

key_set = set(data_dict.iterkeys())
key_set.difference_update([not_wanted_key])
other_key = key_set.pop()

although the loss of clarity compared to the one liner can't be
worth the miniscule benefit in this case.
 
J

Jean Daniel

Building on the answers of the others, a simple one liner, no side
effect, not the fastest I guess:
d={'a': 'bob', 'b': 'stu'}
set( d.keys() ).difference( [ 'a' ] ).pop()
'b'

Note the square brackets for the parameter of difference(). 'The
string 'a' and the list [ 'a' ] are both iterable but really are
different.
 
V

Vlastimil Brom

Thanks for the elaboration;
in retrospect, given the simple requirement, that there are only two
dict keys, one of which is know and the other to be determined, maybe
just the direct dict methods are appropriate, e.g.

d = {'a': 'bob', 'b': 'stu'}
d_copy = dict(d)
d_copy.pop("a")
'bob'
d_copy.popitem()
('b', 'stu')

vbr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top