views:

486

answers:

1

Is there a better way to do this in python, or rather: Is this a good way to do it?

x = ('a', 'b', 'c')
y = ('d', 'e', 'f')
z = ('g', 'e', 'i')

l = [x, y, z]

s = set([e for (_, e, _) in l])

I looks somewhat ugly but does what i need without writing a complex "get_unique_elements_from_tuple_list" function... ;)

edit: expected value of s is set(['b','e'])

+14  A: 

That's fine, that's what sets are for. One thing I would change is this:

s = set(e[1] for e in l)

as it enhances readability. Note that I also turned the list comprehension into a generator expression; no need to create a temporary list.

balpha
+1 "enhances readability"
Jarret Hardie