I'm still learning Python and tried writing this code in a different way than solution. Could you please take a look and let me know if there are any improvements or better practices I should follow?
user_input = input("Enter items seprated by commas: ")
Hi Bijal, that's a smart solution! I like how you have thought about the solution to count the occurrences.
You need to fix one thing though whch is to apply the strip() method to each item so any padded space is removed from the items. You could do that by replacing line 6 with this:
updated = [item.strip() for item in user_input.split(',')]
The input string should be split into a list just once before any loops, rather than repeatedly inside the loop. Also, modifying a list while iterating over it, such as using remove(), can cause unintended issues like skipping elements. It's generally more reliable to build a new list of unique items instead of removing duplicates from the original list in place.
Using count() inside a loop is not very efficient, especially for longer lists. A more typical approach would involve keeping track of which items have already been seen, for example with a set, to filter duplicates more effectively. Lastly, consider using more descriptive variable names to make the code easier to understand and maintain.
Overall, it's good that you're exploring different ways to write the solution. Refining how you structure the logic will make your code more reliable and readable as you progress.
Thanks for posting. It's a practical exercise that introduces useful concepts like string manipulation and basic data cleaning. The instructions are clear, though it may be helpful to clarify a few points for beginners.
It's worth specifying whether the program should preserve the original order of items after removing duplicates, as that affects how the logic is implemented. Also, since user input may contain extra spaces (e.g., "apple, banana ,orange"), trimming whitespace from each item before processing would lead to more consistent results.
Your guide might benefit from a brief mention of common pitfalls, such as modifying a list while iterating over it, or using inefficient methods like repeatedly calling count() on a list. These are small but important details that help reinforce good coding habits early on.
Anyway, it's a solid introduction to real-world data handling.
I'm still learning Python and tried writing this code in a different way than solution. Could you please take a look and let me know if there are any improvements or better practices I should follow?
user_input = input("Enter items seprated by commas: ")
print(user_input)
count = 0
for item in user_input:
updated = user_input.split(",")
for num in updated:
appered = updated.count(num)
if appered > 1:
updated.remove(num)
print(updated)
count += 1
print(f"{count} duplicate(s) were removed")
Thanks
Hi Bijal, that's a smart solution! I like how you have thought about the solution to count the occurrences.
You need to fix one thing though whch is to apply the strip() method to each item so any padded space is removed from the items. You could do that by replacing line 6 with this:
updated = [item.strip() for item in user_input.split(',')]
The input string should be split into a list just once before any loops, rather than repeatedly inside the loop. Also, modifying a list while iterating over it, such as using remove(), can cause unintended issues like skipping elements. It's generally more reliable to build a new list of unique items instead of removing duplicates from the original list in place.
Using count() inside a loop is not very efficient, especially for longer lists. A more typical approach would involve keeping track of which items have already been seen, for example with a set, to filter duplicates more effectively. Lastly, consider using more descriptive variable names to make the code easier to understand and maintain.
Overall, it's good that you're exploring different ways to write the solution. Refining how you structure the logic will make your code more reliable and readable as you progress.
Thank you for taking the time to review the code and share your feedback. I’ll carefully consider your suggestions moving forward.
My solution:
def remove_duplicates():
user_input = input("Enter items separated by commas: ")
lst = [item.strip() for item in user_input.split(",")]
original_length = len(lst)
cleaned_list = sorted(set(lst))
cleaned_length = len(cleaned_list)
duplicates = original_length - cleaned_length
print(f"\nCleaned lst (duplicates removed): {cleaned_list}")
print(f"{duplicates} duplicate(s) were removed.")
remove_duplicates()
Thanks for posting. It's a practical exercise that introduces useful concepts like string manipulation and basic data cleaning. The instructions are clear, though it may be helpful to clarify a few points for beginners.
It's worth specifying whether the program should preserve the original order of items after removing duplicates, as that affects how the logic is implemented. Also, since user input may contain extra spaces (e.g., "apple, banana ,orange"), trimming whitespace from each item before processing would lead to more consistent results.
Your guide might benefit from a brief mention of common pitfalls, such as modifying a list while iterating over it, or using inefficient methods like repeatedly calling count() on a list. These are small but important details that help reinforce good coding habits early on.
Anyway, it's a solid introduction to real-world data handling.