coder1234
coder1234

Reputation: 51

how to replace elements of list with elements of dictionary

I am trying to create a program using python that replaces shorthand names for days of the week with their full names.

This is the code I have done:

# Write your format_days function here.
def format_days(days):
    REPLACEMENTS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday'
   }

   for i in REPLACEMENTS:
       if i in days:
          days = days.replace(i,REPLACEMENTS[i])

  answer = days

  return days

if __name__ == '__main__':
    # You can use this to test your function.
    # Any code inside this `if` statement will be ignored by the automarker.

    # Run your `format_days` function with the first example in the question.
    answer = format_days(['Mon', 'Wed', 'Fri'])
    print(answer)

    # Run your `format_days` function with the second example in the question.
    answer = format_days(['Sat', 'Fun', 'Tue', 'Thu'])
    print(answer)

This is what I want the code to do:

['Monday', 'Wednesday', 'Friday']
['Saturday', 'Tuesday', 'Thursday']

And this is what my code says:

Traceback (most recent call last):
  File "program.py", line 24, in <module>
    answer = format_days(['Mon', 'Wed', 'Fri'])
  File "program.py", line 14, in format_days
    days = days.replace(i,REPLACEMENTS[i])
AttributeError: 'list' object has no attribute 'replace'

I am guessing this is because I am trying to replace elements of a list with elements of a dictionary? I am not sure.

I also need to remove all elements that are not days of the week ('Fun') from the output.

Thank you in advance! :)

Upvotes: 0

Views: 65

Answers (1)

So you are mixing up which DAYS, days, and day you are talking about. Also by using a for variable in list loop, you are creating a variable and losing the index you are trying to update.

Update your loop like so

for index in range(len(days)): # <-- loop through range to get the index
   if days[index] in DAYS: # <--- the index is accessed to get the key in the dictionary
      days[index] = DAYS[days[index]] # <-- the reason we need the index is to assign the answer

The other problem I see is that you assign days to answer and then immediately return answer. This means you can skip that step and just return days instead.

# answer = days
# return answer
return days # <-- no need for an extra variable.

Final Code of the function.

def format_days(days):
    DAYS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday',
    }

    for index in range(len(days)):
       if days[index] in DAYS:
          days[index] = DAYS[days[index]]

    return days

Edit to remove non days from list.

The easiest way to do this would be to actually use a new variable when running the loop and building the answer. So I will introduce your answer variable back in, but before we run the loop now.

def format_days(days):
    DAYS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday',
    }

    answer = [] # <-- new variable

    for index in range(len(days)):
       if days[index] in DAYS:
          answer.append(DAYS[days[index]]) # <-- appending only the found items

    return answer

Now if something in the list is not in the dictionary, it will just be skipped.

Upvotes: 1

Related Questions