Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree
Asher Orr
Python Development Techdegree Student 3,678 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 32,410 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
Asher Orr
Python Development Techdegree Student 3,678 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 32,410 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!