Search for unique elements in tuples in a python list

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

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

l = [x, y, z]

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

I look a little ugly, but I do what I need without writing the complicated function get_unique_elements_from_tuple_list ...;)

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

+3
source share
1 answer

Well, what are they for. I would change 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.

+22
source

Source: https://habr.com/ru/post/1714207/


All Articles