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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:58:55+00:00 2026-05-30T11:58:55+00:00

I realise the info to answer this question is probably already on here, but

  • 0

I realise the info to answer this question is probably already on here, but as a python newby I’ve been trying to piece together the info for a few weeks now and I’m hitting some trouble.

this question Python "join" function like unix "join" answers how to do a join on two lists easily, but the problem is that dictreader objects are iterables and not straightforward lists, meaning that there’s an added layer of complications.

I basically am looking for an inner join on two CSV files, using the dictreader object. Here’s the code I have so far:

def test(dictreader1, dictreader2):
    matchedlist = []
    for dictline1 in dictreader1:
            for dictline2 in dictreader2:
                if dictline1['member']=dictline2['member']:
                    matchedlist.append(dictline1, dictline2)
                else: continue
    return matchedlist

This is giving me an error at the if statement, but more importantly, I don’t seem to be able to access the [‘member’] element of the dictionary within the iterable, as it says it has no attribute “getitem“.

Does anyone have any thoughts on how to do this? For reference, I need to keep the lists as iterables because each file is too big to fit in memory. The plan is to control this entire function within another for loop that only feeds it a few lines at a time to iterate over. So it will read one line of the left hand file, iterate over the whole second file to find a member field that matches and then join the two lines, similar to an SQL join statement.

Thanks for any help in advance, please forgive any obvious errors on my part.

  • 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-30T11:58:56+00:00Added an answer on May 30, 2026 at 11:58 am

    A few thoughts:

    • Replace the = with ==. The latter is used for equality tests; the former for assignments.

    • Add a line a the beginning, dictreader2 = list(dictreader2). That will make it possible to loop over the dictionary entries more than once.

    • Add a second pair of parenthese to matchedlist.append((dictline1, dictline2)). The list.append method takes just one argument, so you want to create a tuple out of dictline1 and dictline2.

    • The final else: continue is unnecessary. A for-loop will automatically loop for you.

    • Use a print statement or somesuch to verify that dictline1 and dictline2 are both dictionary objects that have member as a key. It could be that your function is correct, but is being called with something other than a dictreader object.

    Here is a worked out example using a list of dicts as input (similar to what a DictReader would return):

    >>> def test(dictreader1, dictreader2):
            dictreader2 = list(dictreader2)
            matchedlist = []
            for dictline1 in dictreader1:
                for dictline2 in dictreader2:
                    if dictline1['member'] == dictline2['member']:
                        matchedlist.append((dictline1, dictline2))
            return matchedlist
    
    >>> dr1 = [{'member': 2, 'value':'abc'}, {'member':3, 'value':'def'}]
    >>> dr2 = [{'member': 4, 'tag':'t4'}, {'member':3, 'tag':'t3'}]
    >>> test(dr1, dr2)
    [({'member': 3, 'value': 'def'}, {'member': 3, 'tag': 't3'})]
    

    A further suggestion is to combine the two dictionaries into a single entry (this is closer to what an SQL inner join would do):

    >>> def test(dictreader1, dictreader2):
            dictreader2 = list(dictreader2)
            matchedlist = []
            for dictline1 in dictreader1:
                for dictline2 in dictreader2:
                    if dictline1['member'] == dictline2['member']:
                        entry = dictline1.copy()
                        entry.update(dictline2)
                        matchedlist.append(entry)
            return matchedlist
    
    >>> test(dr1, dr2)
    [{'member': 3, 'tag': 't3', 'value': 'def'}]
    

    Good luck with your project 🙂

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

Sidebar

Related Questions

I realise there are a fair few questions on here already with regard to
This question may seem blindingly obvious and I realise I am putting myself up
Please forgive the newb question, but I am not finding answers elsewhere... probably because
UPDATE 10/19/2010 I know I asked this question a while ago, but the workarounds
Firstly, I realise that this is a very similar question to this one: Which
I realise there is some other questions and info about replication and syncing but
I realise this is a bit vague but hoping someone might be able to
I've done some searching on this, but I cannot find info. I'm building an
I realise that the Java 8 lambda implementation is subject to change, but in
I realise there might be similar questions but I couldn't find one that was

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.