Discussion about this post

User's avatar
Bijal's avatar

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

Expand full comment
VINOD VIJAYAN's avatar

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()

Expand full comment
4 more comments...

No posts