Home > Back-end >  Returning a title cased list and removing duplicates
Returning a title cased list and removing duplicates

Time:07-13

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?

CodePudding user response:

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]))
  • Related