loading...

Set Operation "Tricks" With Python

jorgee97 profile image Jorge Gomez ・3 min read

Hey guys, as I have been working with python lately and I'm trying to learn as much as I can, and I just wanted to share some of my learning experiences with you all.

Note: Most people may know these "tricks" but as a refresher I just wanted to share it and maybe help other devs in the future.

Let's begin with the definition of a Set in python:

A set is an unordered collection of unique elements

Here is the official python.org link: https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset

Knowing that may give you an idea of what Sets can do for you, or how can they help you. I will give you some examples:

Let's say you have a list with items and you want to get rid of the repeated ones. I know one brute force way, which is iterating the list and then adding the elements to another array and finally you got your list with unique items. But what about this:

items = [1, 2, 3, 3, 4, 5, 2, 7]
print(set(items)) # You will get this => {1, 2, 3, 4, 5, 7}

So you see how easy was that, but there is a problem, if you read the link above, you will know that the sets are not mutable, that means that you cannot treat them like a list where you can index and then modify the values. So in order to edit a set, you need to transform it back into a list or a dictionary wherever you are working with.

items = [1, 2, 3, 3, 4, 5, 2, 7]
my_immutable_set = set(items)
unique_items = list(my_immutable_set)
print(unique_items) # You will get this => [1, 2, 3, 4, 5, 7]

That is how you get a list with only unique items. Pretty straight forward right?

Ok, let's now move to the tricks

Bitwise operator AND(&)

Let's say you have 2 sets, and you want to get only the items that are in both of the sets, you can do something like doing a for and asking if the item is in the other set, but that may be tedious to read, or write, what if we can use sets? Well sets got you cover:

s = set({1, 2, 3, 4})
s2 = set({7, 3, 9, 10})

print(s & s2) # this will print => {3}

Bitwise operator XOR(^)

What If we want only the values that are not repeated in both sets?

s = set({1, 2, 3, 4})
s2 = set({7, 3, 9, 10})

print(s ^ s2) # this will print => {1, 2, 4, 7, 9, 10}

Bitwise operator OR(|)

Now, what if we want to combine one set with the other, in a list you can use the sum(+) operator, but with sets you can't do that, so here is how it goes

s = set({1, 2, 3, 4})
s2 = set({7, 3, 9, 10})

print(s | s2) # this will print => {1, 2, 3, 4, 7, 9, 10}

Last "trick"

What if you want to delete the repeated elements that intercepts in one of the sets? Well you can use the subtract(-) operator like so:

s = set({1, 2, 3, 4})
s2 = set({7, 3, 9, 10})

print(s - s2) # this will print => { 1, 2, 4 }

You can notice that the common element between the two sets where deleted and now you have a new set with unique items, that doesn't exist into the other one. I hope I made myself understood because this was a little bit tricky to explain.

Last words

I hope you enjoyed this little post, and find it helpful, if you have any suggestion or questions feel free to share your thoughts down bellow in the comments. I will try to share my experiences with python as much as I can, I find this method of "teaching" and learning really good both for me and for some readers that find this helpful or interesting.

Posted on Sep 27 '19 by:

jorgee97 profile

Jorge Gomez

@jorgee97

I'm a software developer, looking forward to be a great developer.

Discussion

markdown guide