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 6551223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:18:37+00:00 2026-05-25T12:18:37+00:00

i am new to the concept of dictionaries in python. I have a csv

  • 0

i am new to the concept of dictionaries in python.
I have a csv file with multiple columns and i want to create a dictionary such that keys are taken from 1st column and values from the second and a key:value pair is made for all rows of those two columns.
The code is as follows:

    if __name__=="__main__":
reader = csv.reader(open("file.csv", "rb"))
for rows in reader:
        k = rows[0]
        v = rows[1]
        mydict = {k:v}
print (mydict)

problem: The output returned is only for “last” or “bottom most” row of the first two columns i.e. {‘12654′:’18790’}. i want the dictionary to contain all 100 rows of the first two columns in this format. How to do that? can i run some loop on the row numbers for the first two columns to do that…i dont know how.

  • 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-25T12:18:38+00:00Added an answer on May 25, 2026 at 12:18 pm
    if __name__=="__main__":
        mydict = {}
        reader = csv.reader(open("file.csv", "rb"))
        for rows in reader:
            k = rows[0]
            v = rows[1]
            mydict[k] = v
        print mydict
    

    Here:

    mydict = {k:v}
    

    You were making new dictionary in every iteration, and the previous data has been lost.

    Update:

    You can make something like this:

    mydict = {}
    L = [(1, 2), (2, 4), (1, 3), (3, 2), (3, 4)]
    for el in L:
        k, v = el
        if not k in mydict:
            mydict[k] = [v]
        else:
            mydict[k].append(v)
    
    print mydict
    
    >>> 
    {1: [2, 3], 2: [4], 3: [2, 4]}
    

    This way, each value of the same key will be stored

    Your code will be:

    if __name__=="__main__":
        mydict = {}
        reader = csv.reader(open("file.csv", "rb"))
        for i, rows in enumerate(reader):
            if i == 0: continue
            k = rows[0]
            v = rows[1]
            if not k in mydict:
                mydict[k] = [v]
            else:
                mydict[k].append(v)
    
        print mydict
    

    Update2: You mean?

    for k, v in mydict.items():
        print "%s: %s" % (k, v)
    
    >>>
    1: [2, 3]
    2: [4]
    3: [2, 4]
    

    Update3:

    This should work:

    if __name__=="__main__":
            mydict = {}
            reader = csv.reader(open("file.csv", "rb"))
            for i, rows in enumerate(reader):
                if i == 0: continue
                k = rows[0]
                v = rows[1]
                if not k in mydict:
                    mydict[k] = [v]
                else:
                    mydict[k].append(v)
    
            print mydict
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i want to try a new concept that involves deep modifications of the
I'm fairly new to the DI concept, but I have been using it to
New to xml. Looking for XPath to search a xml file with python ElementTree
I've just read an article that supposedly introduced me to a new concept: Up
Where a new system concept or new technology is used, one has to build
This concept is new to me, and a colleague suggested it. Sadly, I had
I'm not brand new to the concept of unit testing but at the same
This concept is a new one for me -- I first came across it
I new to Scrum and while I understand the team concept behind the Sprints,
HI Magento introduced a new concept of widget in magento 1.4 , my question

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.