This Remove List Duplicates lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
A set drops duplicates and forgets the order. A loop that records what it has seen keeps the first occurrence in the original order.
Use the set when order does not matter. Use the loop when it does.
Example 1: Start here
dict.fromkeys(skills) is another way to keep the first of each value, on Python 3.7 and newer.
skills = ["Python", "SQL", "Python", "Git"] # a list; the values stay in this order
seen = [] # a list; the values stay in this order
for skill in skills: # repeat the indented lines once per value
if skill not in seen: # run the indented lines only when this is true
seen.append(skill) # add one item at the end of the list
print(seen)
Example 2: Keep the first order
Checking the result list keeps the first time each skill appears.
skills = ["Python", "SQL", "Python", "Git"] # a list; the values stay in this order
seen = [] # a list; the values stay in this order
for skill in skills: # repeat the indented lines once per value
if skill not in seen: # run the indented lines only when this is true
seen.append(skill) # add one item at the end of the list
print(seen)
Example 3: A set is shorter
dict.fromkeys keeps the first copy of each key and remembers the order.
print(list(dict.fromkeys(["Python", "SQL", "Python", "Git"]))) # print writes this; the quotes are not shown
Example 4: Case-insensitive
Compare a lowercase copy so Python and python count as the same skill.
skills = ["Python", "python", "SQL"] # a list; the values stay in this order
seen = [] # a list; the values stay in this order
for skill in skills: # repeat the indented lines once per value
if skill.lower() not in [item.lower() for item in seen]: # run the indented lines only when this is true
seen.append(skill) # add one item at the end of the list
print(seen)
