Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning a title cased list and removing duplicates

I’m new to programming, and I picked Python to start learning first because there was a sale on some resources for learning it on Humble Bundle. That put me on the PyBites website, where I’m doing a programming exercise to manipulate a list. Here is the code I am having trouble with.

NAMES = ['arnold schwarzenegger', 'alec baldwin', 'bob belderbos',
         'julian sequeira', 'sandra bullock', 'keanu reeves',
         'julbob pybites', 'bob belderbos', 'julian sequeira',
         'al pacino', 'brad pitt', 'matt damon', 'brad pitt']
import re

def dedup_and_title_case_names(names):
    """Should return a list of title cased names,
       each name appears only once"""
    name_list = []
    name_set = set()
    for name in names:
        if name in name_set == set(names):
            pass
        else:
            name_set.add(name)
            name_list.append(name.title())
    return name_list

So PyBites does a check to determine if your code is working. In the check, I have one pass and six failures (because I’ve been stuck on this third of the exercise), I believe, based on running this in PyCharm, that it’s keeping the first duplicate that shows up (bob belderbos), but it does not keep the second duplicate (brad pitt).

Any ideas what’s going wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this.

def dedup_and_title_case_names(names):
"""Should return a list of title cased names,
   each name appears only once"""
   
return list(set([name.title() for name in names]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading