gray key
Photo by Kelly Sikkema on Unsplash

Let us assume you are given a list and you need to remove all the duplicate elements from the list. One common way to remove duplicates from a list is to use the set() function.

We can simply convert the list to a set and then convert the set back to a list so that we are still working with a list.

For example, let us take the following list that has duplicate elements and see this implementation in action:

The new list is not equal to the original list, since we have removed the duplicates from it.

However, there is another way to remove the duplicates from a list without using the set() function that does not involve the usage of a for loop and saving in a new list only the elements that have not been there yet.

We can do that by converting the list into keys of a dictionary and then converting the dictionary keys back to a list. Since keys in a dictionary are unique, there is not going to be any duplicate element left:

This is something that you may have not had the chance to see before, since the set() function is the one that is used pretty often.

I hope you find it useful.