Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:45:02+00:00 2026-05-26T11:45:02+00:00

I have a file that I’m reading in and splitting and pushing into a

  • 0

I have a file that I’m reading in and splitting and pushing into a dictionary. I keep getting an error and I think its because the street addresses contain a comma (,).

Here is my file contents:

axe99:315 W. 115th Street, Apt. 11B:New York:NY:10027
jab44:23 Rivington Street, Apt. 3R:New York:NY:10002
ap172:19 Boxer Rd.:New York:NY:10005
jb23:115 Karas Dr.:Jersey City:NJ:07127
jb29:119 Xylon Dr.:Jersey City:NJ:07127
ak9:234 Main Street:Philadelphia:PA:08990

Here is my code:

f2data = open('ex1.txt')
for line in f2data:
    print line.strip().split(':')
    city_dict = dict(item.split(':') for item in line.strip('\n').split(','))
    print city_dict

It keeps throwing this error:

['jk43', '23 Marfield Lane', 'Plainview', 'NY', '10023']

city_dict = dict(item.split(':') for item in line.strip('\n').split(','))
ValueError: dictionary update sequence element #0 has length 5; 2 is required

I’m new to Python and really have no idea what this error means, any thoughts greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:45:03+00:00Added an answer on May 26, 2026 at 11:45 am

    You are getting the error because you are trying to push a list with 5 items into a dictionary constructor when it only takes two items (in the dict constructor python thinks you are using):

    here is a list of possible constructors

    dict(one=1, two=2)
    dict({'one': 1, 'two': 2})
    dict(zip(('one', 'two'), (1, 2)))
    dict([['two', 2], ['one', 1]])
    

    taken from python docs

    you will need to split your string differently or find a better way of formatting your data.
    If you are reading from a csv file, refer to Michael Hoffman’s answer, which is the correct way to read from a csv.

    If not, please specify what you are doing and why.


    EDIT: Goal added, answer updated

    for input:

    axe99:315 W. 115th Street, Apt. 11B:New York:NY:10027
    jab44:23 Rivington Street, Apt. 3R:New York:NY:10002
    ap172:19 Boxer Rd.:New York:NY:10005
    jb23:115 Karas Dr.:Jersey City:NJ:07127
    jb29:119 Xylon Dr.:Jersey City:NJ:07127
    ak9:234 Main Street:Philadelphia:PA:08990
    

    the code:

    city_dict =  {}
    for line in open('ex1.txt'):
        if item.split(':')[3] in city_dict:
            city_dict[item.split(':')[3]] += 1
        else:
            city_dict[item.split(':')[3]] = 1
    
    print (city_dict)
    

    will output what you desire:

    {'NY': 3, 'NJ': 2, 'PA': 1}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that apparently contains some sort of dump of a keep-alive
I have a file that I read into a char array. The char array
I have a file that describes input data, which is split into several other
I have a file that needs to be refreshed every days, linked into a
I have a file that is organized into columns, with data that needs to
I have a file that's been modified recently. Rather than looking into each revision,
I have a file that I read in into R and is translated to
I have csv file that looks like this: artist,year,id,video_name,new_video_id,file_root_name,video_type ,,,,,, Clay Aiken,1,clay_aiken,Sorry Seems To
I have a file that looks something like this def foo bar {{ //
I have a file that I want to include in Python but the included

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.