Recap - Remove Duplicates
Part of the Logic & Flow section of Coddy's Python journey — lesson 32 of 78.
Challenge
EasyIn Python, you can convert a list to a set using the set() function. This is called casting. Casting to a set transforms the list into a set, which contains only unique elements.
Example:
my_list = [1, 2, 2, 3, 4, 4, 5]
my_set = set(my_list)
print(my_set)
# Output: {1, 2, 3, 4, 5}Write a function named remove_duplicates that takes a list of numbers as an argument and returns a new list with duplicates removed while preserving the original order of elements.
Example Input:
numbers = [1, 2, 2, 3, 4, 4, 5]Example Output:
[1, 2, 3, 4, 5]Use the properties of sets to simplify your solution while keeping the order intact!
Try it yourself
def remove_duplicates(numbers):
# Write code hereAll lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter