Overview – Python Coding Challenge
Let’s start with the Python Coding Challenge to solve a real-world problem. There are many real-world problems; today we will get one of them i.e., the event’s guest list with tons of duplicate names. Let’s see how we can solve that with Python coding skills.
What’s Next? – Python Coding Challenge
In this article, I’ll walk you through how you will solve the real-world problem of getting a list of unique names along with arranging them into an alphabetical order.
Table of Contents
Watch Video Explanation
Prerequisites you need to know:
What is Python List?
Python list is the collection of ordered elements. It allows duplicate elements and supports mutable features, meaning once the element is defined, it can be changed.
What is Python Set?
Python set is the collection of unordered + unique elements, meaning it stores unique elements (no duplicate elements). From the property, it is immutable, meaning once elements are defined, cannot be changed.
Introduction: A Real-World Problem
Question: Let’s imagine You’ve got a guest list with many duplicate names. Write a program to clean up this list by ensuring every guest is counted just once and sorting the list names Alphabetically.
Breaking Down the Problem
- Remove the duplicate names
- Sort the list names Alphabetically
- Print the clean list
Code Setup + Explanation
Step: 1 – Remove the duplicate names.
guest_names = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Diana", "John", "Smith", "John"]
unique_names = set(guest_names)
print("Unique Names:", unique_names)
Output: Unique Names: {'Alice', 'Diana', 'Bob', 'Charlie', 'John', 'Smith'}
Explanation: The above guest list i.e., a Python set contains unique names (all names counted once) because the Python set doesn’t store duplicate elements.
Step: 2 – Sort the list names Alphabetically.
sorted_names = sorted(unique_names)
print("Sorted Names:", sorted_names)
Output: Sorted Names: ['Alice', 'Bob', 'Charlie', 'Diana', 'John', 'Smith']
Explanation: The above guest list is a Python list because it is stored inside square brackets []. The guest names were sorted alphabetically by using the Python sorted() method.
Since Python Set does not have any method to sort the elements, therefore; the Python list method sorted() is used to sort the elements.
Step: 3 – Print the clean list.
print("Final List:", sorted_names)
Output: Final List: ['Alice', 'Bob', 'Charlie', 'Diana', 'John', 'Smith']
A Twist! A Fun! A Surprise!
Step: 4 – Normalize the case and remove duplicate names.
unique_names = set(name.lower() for name in guest_names)
print("Unique Names:", unique_names)
Output: Unique Names: {'diana', 'bob', 'alice', 'john', 'smith', 'charlie'}
Explanation: Python for loop is used to iterate each name one by one from the guest_names variable and with each iteration, each name is converted to the lowercase by using the lower() method.
Step: 5 –. Sort the list and print Final the List
sorted_names = sorted(unique_names)
print("Sorted + Final List:", sorted_names)
Output: Sorted + Final List: ['alice', 'bob', 'charlie', 'diana', 'john', 'smith']
Now it’s YOUR call!
What life problem would you want to solve with Python? Let’s jump into the YouTube channel – codingstreets and share your real-world problem and also what ways you would gonna follow to solve it!
Conclusion – Python Coding Challenge
So how was it? Were you shocked by seeing how easily by using Python Set and Python List, we solved a real-world problem smartly? This is how effectively we can use Python to deal with life problems.