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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:06:43+00:00 2026-06-18T19:06:43+00:00

using Python 3.3.0, I created a dictionary from a csv-file (header: ID;Col1;Col2;Col3;Col4;Col5 ): ID;Col1;Col2;Col3;Col4;Col5

  • 0

using Python 3.3.0, I created a “dictionary” from a csv-file (header: ID;Col1;Col2;Col3;Col4;Col5):

ID;Col1;Col2;Col3;Col4;Col5
15345;1;1;nnngngn;vhrhtnz;latest
12345;12;8;gnrghrtthr;tznhltrnhklr;latest
90834;3;4;something;nonsens;latest
12345;34;235;dontcare;muhaha;oldone

with code

file = "test.csv" 
csv_file = csv.DictReader(open(file, 'r'), delimiter=';', quotechar='"')

and I wanted to copy the lines with ID = 12345 into a new dictionary, NOT into a file.
I really nedded to copy into a dictionary, NOT a list, because I wanted to be able to address the column names directly.
I tried this by doing

cewl = {}
for row in csv_file:
   if row['ID'] == '12345':
   cewl.update(row)
print(cewl)

Output is:

{'ID': '12345', 'Col1': '34', 'Col2': '235', 'Col3': 'dontcare', 'Col4': 'muhaha', 'Col5': 'oldone'}

My problem:
Only the second line with ID=12345 gets copied, the first one is omitted, I don’t know why.

If I try this by copying into a new list (just for testing purposes), everything works fine:

cewl = []
for row in csv_file1:
if row['ID'] == '12345':
    cewl.append(row)
print(cewl)

Output is :

[{'Col3': 'gnrghrtthr', 'Col2': '8', 'Col1': '12', 'Col5': 'latest', 'Col4': 'tznhltrnhklr', 'ID': '12345'}, 
{'Col3': 'dontcare', 'Col2': '235', 'Col1': '34', 'Col5': 'oldone', 'Col4': 'muhaha', 'ID': '12345'}]

I don’t know why this isn’t working by copying into the new dictionary…there doesn’t seem to be a method like .add or .append for dictreader.

How can I copy my data into a new dictionary without missing any lines ?

  • 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-06-18T19:06:45+00:00Added an answer on June 18, 2026 at 7:06 pm

    What is the expected output? The behaviour is perfectly normal for a dict; you are replacing the values for each key with a new value.

    If you wanted the values to be lists of the values for each matching row, it’s easier to use a defaultdict with a list factory:

    from collections import defaultdict
    
    cewl = defaultdict(list)
    
    for row in csv_file:
       if row['ID'] == '12345':
           for k, v in row.items():
               cewl[k].append(v)
    
    print(cewl)
    

    This outputs:

    defaultdict(<class 'list'>, {'Col1': ['12', '34'], 'ID': ['12345', '12345'], 'Col2': ['8', '235'], 'Col5': ['latest', 'oldone'], 'Col4': ['tznhltrnhklr', 'muhaha'], 'Col3': ['gnrghrtthr', 'dontcare']})
    

    A defaultdict is a subclass of dict,so print(cewl['Col1']) will print ['12', '34'].

    When you use .update() you effectively do this:

    for k, v in row.items():
        cewl[k] = v
    

    e.g. set each key in cewl to the value found in the row being processed. When the last row is being processed, it’s values overwrite the values of previous rows.

    If you want to filter out just the rows that match a certain ID criteria, then adding them to a list is just perfectly fine. You then loop over the matched results to process them:

    for row in cewl:
        # do something with matched row
    

    or you can build a generator filter that you wrap around your DictReader() to do the filtering for you, so you don’t need to build the list in memory:

    def rowfilter(reader, id):
        for row in reader:
            if row['ID'] == id:
                yield row
    
    for row in rowfilter(csv_file, '12345'):
        # do something with matched row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a python server on port 8000 using python -m SimpleHTTPServer . When
I'm using Python 2.x [not negotiable] to read XML documents [created by others] that
The documentation of my project (using Python 3.2) is created with Sphinx (1.1.3) and
I am a beginner programmer, using python on Mac. I created a function as
I am practicing with Python using Pyramid framework. I created a py, myfuncs.py ,
I created a python .py with some sort methods. I'm using numpy so that
I created a little web spider in Python which I'm using to collect URLs.
I'm using App Engine, SDK 1.6.3 with Python 2.7. I've created a model like
I am using Python 2.6.2. I have a dictionary, graph which has tuple (source,
Using Python's Networkx library, I created an undirected graph to represent a relationship network

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.