LinkedIn Reddit icon

Dunder Doc

by Valdir Stumm Jr

Drop Duplicates from a List in Order

Let’s say you have a list containing all the URLs extracted from a web page and you want to get rid of duplicate URLs. The most common way of achieving that might be building a set from that list, given that such operation automatically drops the duplicates. Something like: >>> urls = [ 'http://api.example.com/b', 'http://api.example.com/a', 'http://api.example.com/c', 'http://api.example.com/b' ] >>> set(urls) {'http://api.example.com/a', 'http://api.example.com/b', 'http://api.example.com/c'} The problem is that we just lost the original order of the list.