Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extracting item from list of tuples?

Reply
Thread Tools

Extracting item from list of tuples?

 
 
Roy Smith
Guest
Posts: n/a
 
      09-05-2010
I'm using httplib, and want to get the Location header from the
response. The getheaders() method gives you back a list of (name,
value) tuples. It would be a lot more convenient if it gave you back a
dict, but it is what it is.

Anyway, I came up with:

location = [t[1] for t in headers if t[0] == 'location'][0]

which works, but is getting on towards the cryptic end of things. Is
there a better idiom for this?
 
Reply With Quote
 
 
 
 
Thomas Jollans
Guest
Posts: n/a
 
      09-05-2010
On Sunday 05 September 2010, it occurred to Roy Smith to exclaim:
> I'm using httplib, and want to get the Location header from the
> response. The getheaders() method gives you back a list of (name,
> value) tuples. It would be a lot more convenient if it gave you back a
> dict, but it is what it is.


It may not be a dict, but you can always make it one:

>>> L = [('a',1), ('b', 42), ('c', 0)]
>>> dict(L)

{'a': 1, 'c': 0, 'b': 42}
>>>



 
Reply With Quote
 
 
 
 
Philip Semanchuk
Guest
Posts: n/a
 
      09-05-2010

On Sep 5, 2010, at 1:09 PM, Roy Smith wrote:

> I'm using httplib, and want to get the Location header from the
> response. The getheaders() method gives you back a list of (name,
> value) tuples. It would be a lot more convenient if it gave you
> back a
> dict, but it is what it is.
>
> Anyway, I came up with:
>
> location = [t[1] for t in headers if t[0] == 'location'][0]
>
> which works, but is getting on towards the cryptic end of things. Is
> there a better idiom for this?


Are you concerned about efficiency? If it's not important, being less
terse would make it less cryptic:
location = None
for name, value in headers:
if name == "location":
location = value
break

If you prefer terse, IMHO this is less cryptic (but untested):

location = [value for name, value in headers if name == 'location']
[0]


Be careful if you decide to turn the list of tuples into a dictionary.
HTTP headers can repeat and straightforward code to turn the (name,
value) pairs into a dict could end up overwriting data.

Cheers
P
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-05-2010
Philip Semanchuk wrote:

>
> On Sep 5, 2010, at 1:09 PM, Roy Smith wrote:
>
>> I'm using httplib, and want to get the Location header from the
>> response. The getheaders() method gives you back a list of (name,
>> value) tuples. It would be a lot more convenient if it gave you
>> back a
>> dict, but it is what it is.
>>
>> Anyway, I came up with:
>>
>> location = [t[1] for t in headers if t[0] == 'location'][0]
>>
>> which works, but is getting on towards the cryptic end of things. Is
>> there a better idiom for this?

>
> Are you concerned about efficiency? If it's not important, being less
> terse would make it less cryptic:
> location = None
> for name, value in headers:
> if name == "location":
> location = value
> break


This is actually more efficient because unlike Roy's (and your other
solution) it doesn't build a list, and it doesn't waste cycles in the loop
once the location is found. You can write it as a generator expression like
so:

location = next((value for name, value in headers if name == "location"),
None)

But just converting the headers to a dict is clearer and probably even
faster because it moves the loop from Python to C.

> If you prefer terse, IMHO this is less cryptic (but untested):
>
> location = [value for name, value in headers if name == 'location']
> [0]
>
>
> Be careful if you decide to turn the list of tuples into a dictionary.
> HTTP headers can repeat and straightforward code to turn the (name,
> value) pairs into a dict could end up overwriting data.


 
Reply With Quote
 
Philip Semanchuk
Guest
Posts: n/a
 
      09-05-2010

On Sep 5, 2010, at 1:45 PM, Peter Otten wrote:

> Philip Semanchuk wrote:
>
>>
>> On Sep 5, 2010, at 1:09 PM, Roy Smith wrote:
>>
>>> I'm using httplib, and want to get the Location header from the
>>> response. The getheaders() method gives you back a list of (name,
>>> value) tuples. It would be a lot more convenient if it gave you
>>> back a
>>> dict, but it is what it is.
>>>
>>> Anyway, I came up with:
>>>
>>> location = [t[1] for t in headers if t[0] == 'location'][0]
>>>
>>> which works, but is getting on towards the cryptic end of things.
>>> Is
>>> there a better idiom for this?

>>
>> Are you concerned about efficiency? If it's not important, being less
>> terse would make it less cryptic:
>> location = None
>> for name, value in headers:
>> if name == "location":
>> location = value
>> break

>
> This is actually more efficient because unlike Roy's (and your other
> solution) it doesn't build a list, and it doesn't waste cycles in
> the loop
> once the location is found.


Upon further reflection, though, I realize that my solution is not
equivalent to the original. In the case of duplicated headers, the
original would return the last value of the location header, my code
would return the first. For the HTTP Location header this is only a
theoretical problem because the HTTP spec doesn't provide for sending
more than one.

However if this code were made into a function for retrieving the
value of an arbitrary header (as seems likely), one would have to
decide how to handle repeating headers. Neither my code nor the
original handle that possibility. Nor does a naive conversion into a
dict, as I mentioned before.

Cheers
P

> You can write it as a generator expression like
> so:
>
> location = next((value for name, value in headers if name ==
> "location"),
> None)
>
> But just converting the headers to a dict is clearer and probably even
> faster because it moves the loop from Python to C.
>
>> If you prefer terse, IMHO this is less cryptic (but untested):
>>
>> location = [value for name, value in headers if name ==
>> 'location']
>> [0]
>>
>>
>> Be careful if you decide to turn the list of tuples into a
>> dictionary.
>> HTTP headers can repeat and straightforward code to turn the (name,
>> value) pairs into a dict could end up overwriting data.

>
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting a List from a List of lists Damo Java 4 02-09-2007 01:02 AM
why writing list to file puts each item from list on seperate line? homepricemaps@gmail.com Python 5 12-31-2005 05:21 AM
why writing list to file puts each item from list on seperate line? homepricemaps@gmail.com Python 0 12-31-2005 04:22 AM
List index method for complex list item types? techiepundit@futurepundit.com Python 4 12-31-2005 03:44 AM
Change style of a single row of the item list of datagrid, based on a field value of current item... QUASAR ASP .Net Datagrid Control 6 01-17-2004 07:46 PM



Advertisments