Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add keys to a dictionary with existing data?

The code im trying to create is supposed to have two keys:

["democrats"]
["republicans"]

I’ve nearly got it with all the correct data, I just don’t have the key dictionaries. This is what I’m currently working with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def getPartyUserTwitterUsage(tweetFile):
    import csv 
    
    myFile = open(tweetFile,"r") # opening file in read
    
    csvReader = csv.reader(myFile,delimiter=",") # splitting for ','
    
    next(csvReader) # skipping header
    
    tweetList = {}
    
    repTweet = 0
    demoTweet = 0
    
    
    for row in csvReader: # iterating through file
        if (row[1] == 'R'):
            if (row[0] not in tweetList):
                tweetList[row[0]] = 1
            else:
                tweetList[row[0]] += 1
        
        
        if (row[1] == 'D'):
            if (row[0] not in tweetList):
                tweetList[row[0]] = 1
            else:
                tweetList[row[0]] += 1

    return tweetList

This function:
getPartyUserTwitterUsage("Tweets-2020 (2).csv")

returns:

{'ChrisMurphyCT': 1000,
 'SenBlumenthal': 1000,
 'SenatorCarper': 1000,
 'ChrisCoons': 1000,
 'brianschatz': 1000,
 'maziehirono': 1000,
 'SenatorDurbin': 1000,
 'SenatorHarkin': 1000,
 'lisamurkowski': 1000,
 'JeffFlake': 1000,
 'marcorubio': 1000,
 'MikeCrapo': 958,
 'SenatorRisch': 1000,
 'ChuckGrassley': 1000,
 'SenPatRoberts': 1000,
 'JerryMoran': 1000}

This is the output im expecting:

{'Republicans': {'lisamurkowski': 1000,
  'JeffFlake': 1000,
  'marcorubio': 1000,
  'MikeCrapo': 958,
  'SenatorRisch': 1000,
  'ChuckGrassley': 1000,
  'SenPatRoberts': 1000,
  'JerryMoran': 1000},
 'Democrats': {'ChrisMurphyCT': 1000,
  'SenBlumenthal': 1000,
  'SenatorCarper': 1000,
  'ChrisCoons': 1000,
  'brianschatz': 1000,
  'maziehirono': 1000,
  'SenatorDurbin': 1000,
  'SenatorHarkin': 1000}}

>Solution :

You need to create the keys: Republicans and Democrats before inserting the entries.

...
    tweetList["Republicans"] = {}
    tweetList["Democrats"] = {}
    for row in csvReader: # iterating through file
        if (row[1] == 'R'):
            if (row[0] not in tweetList["Republicans"]):
                tweetList["Republicans"][row[0]] = 1
            else:
                tweetList["Republicans"][row[0]] += 1
        
        
        if (row[1] == 'D'):
            if (row[0] not in tweetList["Democrats"]):
                tweetList["Democrats"][row[0]] = 1
            else:
                tweetList["Democrats"][row[0]] += 1

    return tweetList
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading