'I want to print unique strings from a list in python but it breaks the list

I am trying to fetch the different file paths through this:

for (i, imagePath) in enumerate(imagePaths):
    name = set(imagePath.split(os.path.sep)[-2])

It brings multiple paths that have the same names such as this: Angelina Jolie Angelina Jolie Sam Sam Sam

What I want to do is print the unique ones of them. Like print Angelina Jolie only once. But whatever I try whether it is the unique method, or the set method to convert list to a set it returns something like this. And I am not understanding the logic behind this.

{'l', 'e', 'J', 'g', 'i', ' ', 'o', 'a', 'A', 'n'} {'l', 'e', 'J', 'g', 'i', ' ', 'o', 'a', 'A', 'n'} {'l', 'e', 'J', 'g', 'i', ' ', 'o', 'a', 'A', 'n'} {'l', 'e', 'J', 'g', 'i', ' ', 'o', 'a', 'A', 'n'}

Please help me try to understand why this is happening and what solution should I look for?



Solution 1:[1]

There are two ways to remove duplicates.

list(dict.fromkeys(imagePaths).keys())
# or
list(set(imagePaths)) # If you don't care about order

You don't necessarily have to convert to lists btw.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 12944qwerty