Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get elements of set excluding some

dic = {'a': {1, 2, 3}, 'b': {5, 6, 7}}
print(dic['a'])

How can I get the printed result to exclude 1 (not to remove) to be:

{2,3}
like image 563
gbox Avatar asked Mar 04 '23 18:03

gbox


1 Answers

You could subtract {1} from the inner set:

print(dic['a'] - {1})
{2, 3}

Or equivalently you can use difference:

print(dic['a'].difference({1}))
{2, 3}

You can learn more about the topic on sets — Unordered collections of unique elements

like image 146
yatu Avatar answered Mar 18 '23 10:03

yatu