Getting some element from sets.Set

Andrew McLean andrew-news at andros.org.uk
Mon May 7 09:49:03 EDT 2007


jm.suresh at no.spam.gmail.com wrote:
> In the particular case, I have to read an attribute from any one of
> the elements, which one doesn't matter because this attribute value is
> same across all elements in the set.

Someone else pointed out that there might be better data structures. If 
performance was not an issue one approach would be illustrated by the 
following:

 >>> Q=set(['A','a'])
 >>> list(set(x.upper() for x in Q))
['A']

This has the benefit that it does not assume all the elements of the set 
have the same value of the given attribute.

Again not very efficient:

 >>> list(Q)[0]
'A'

I'm guessing this would be quicker

 >>> iter(Q).next()
'A'



More information about the Python-list mailing list