Reputation: 129
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'])
Upvotes: 5
Views: 9263
Reputation: 50898
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.
Upvotes: 24