Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,409 Points

When updating a dictionary using a for loop, all keys return the same value (despite iterating through multiple values.)

Hi everyone!

I created a function called splitsville. It takes 1 parameter, an address ('100 E Main St, Anywhereville, Oregon, 22222'.)

I split the string into the street, city, state, and zip code.

Now, I'm trying to return a dictionary with the keys:

  • street
  • city
  • state
  • zip_code

... and the corresponding values from the split string. For example: {'street': '100 E Main St', 'city': 'Anywhereville', etc.}

I wrote this code:

def splitsville(address):
    address_values = address.split(", ")
    dictionary = {}
    for value in address_values:
        dictionary.update({"street" : value})
        dictionary.update({"city" : value})
        dictionary.update({"state" : value})
        dictionary.update({"zip_code" : value})
    print(dictionary)

splitsville("100 E Main Street, Anywhereville, Oregon, 22222")

However, only the last value in address_values is associated with each key. My console prints out:

{'street': '22222', 'city': '22222', 'state': '22222', 'zip': '22222'}

Any ideas on why this is happening?

Thank you all so much!

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,004 Points

Hi Asher. I'll tell you why it's happening and let you work on the fix. So remember the "value" in your for loop will only increment once for each iteration of your loop. So the first time all dictionary keys will equal "100 E Main Street" and the second time all keys will equal "Anywhereville" and so on until the last time everything will equal "22222". To help see this move your print inside the loop. You only want to set one key each time through the loop.

Hope this helps Asher and good luck!

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,409 Points

Hey Mark, thanks for your feedback! It's very helpful.

If I understand correctly, setting only one key per loop would mean creating multiple loops. That seemed a little messy to me, so I went with this solution:

def splitsville(address):
    address_values = address.split(", ")
    dictionary = {}
    dictionary.update({"street" : address_values[0]})
    address_values.pop(0)
    dictionary.update({"city": address_values[0]})
    address_values.pop(0)
    dictionary.update({"state": address_values[0]})
    address_values.pop(0)
    dictionary.update({"zip_code": address_values[0]})
    print(dictionary)


splitsville("100 E Main Street, Anywhereville, Oregon, 22222")

It works, too! This prints to the console:

{'street': '100 E Main Street', 'city': 'Anywhereville', 'state': 'Oregon', 'zip_code': '22222'}

Again, thank you for taking the time to answer my question. I appreciate you!

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,004 Points

Great job Asher getting it to work. Thanks for setting the Best answer. It's nice so that others know you have an answer and don't click on it to see if you need help. Also thanks for the update with your solution. Keep at it and good luck!