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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:15+00:00 2026-05-24T20:26:15+00:00

Below is the list of dict using trying to generate dict data = [

  • 0

Below is the list of dict using trying to generate dict

data = [
  {'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8},
  {'date': datetime.date(2011, 8, 15), 'name': u'ab', 'total': 8}, 
  {'date': datetime.date(2011, 8, 16), 'name': u'ab', 'total': 8}, 
  {'date': datetime.date(2011, 8, 17), 'name': u'ab', 'total': 8}, 
  {'date': datetime.date(2011, 8, 18), 'name': u'ab', 'total': 8},
  {'date': datetime.date(2011, 8, 19), 'name': u'ab', 'total': 8}, 
  {'date': datetime.date(2011, 8, 20), 'name': u'ab', 'total': 8}, 
  {'date': datetime.date(2011, 8, 21), 'name': u'ab', 'total': 8},
  {'date': datetime.date(2011, 8, 18), 'name': u'aj', 'total': 0},
  {'date': datetime.date(2011, 8, 14), 'name': u'ap', 'total': 8}, 
  {'date': datetime.date(2011, 8, 15), 'name': u'ap', 'total': 8},
  {'date': datetime.date(2011, 8, 16), 'name': u'ap', 'total': 8},
  {'date': datetime.date(2011, 8, 17), 'name': u'ap', 'total': 8},
  {'name': u'ab', 'l_total': 8, 'type': u'UP'}, 
  {'name': u'ab', 'l_total': 8, 'type': u'PL'},
  {'name': u'fk', 'l_total': 29, 'type': u''},
  {'name': u'jw', 'l_total': 1, 'type': u'PD'}, 
  {'name': u'uk', 'l_total': 16, 'type': u'UP'}, 
  {'name': u'sk', 'l_total': 24, 'type': u'PL'},
  {'name': u'ss', 'l_total': 8, 'type': u'PL'},
  {'name': u'sst', 'l_total': 8, 'type': u'PL'}]



results = collections.defaultdict(dict)

for fetch in data:
        user = fetch['name']
        hrs = fetch['total']
        row = results[user]
        new_y = fetch['type']
        row['new_y'] = fetch['l_total']
        row['user'] = user
        dt_string = str(fetch['date'])
        if dt_string in fetch and row[dt_string] != hr:
          raise Exception, "Contradiction: '%s' on '%s'" % (user, dt_string)
        row[dt_string] = hrs
        row.setdefault('Total', 0)
        row['Total'] += hrs

while generating below output

{u'ap': {'Total': 32, 'user': u'ap', '2011-08-17': 8, 
         '2011-08-16': 8, '2011-08-15': 8, '2011-08-14': 8,
         'up': 8, 'PL':8})

Getting key error,any help really appreciate

Note : Here you find two type of dicts one {‘date’: datetime.date(2011, 8, 14), ‘name’: u’ab’, ‘total’: 8}, and another is {‘name’: u’ab’, ‘total’: 8, ‘type’: u’UP’},. difference between two is keys. in first dict you find date key and another dict type key

  • 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-24T20:26:15+00:00Added an answer on May 24, 2026 at 8:26 pm

    Why shouldn’t you get a KeyError? You’re asking for the value associated with type from the first row of data and there’s no such key.

    EDIT:

    @agf says this is underhelpful, so let me try again.

    The first line of data is

    {'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8}
    

    in the loop fetch (btw, variables should be named after nouns, not verbs; function names are verbs) is set to that first row and the following line is executed:

    new_y = fetch['type']
    

    Well, that line cannot succeed. There is no entry for type in fetch so, as you have seen, Python throws a KeyError. Change the data so there is such an entry or change the code so it doesn’t need it.

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

Sidebar

Related Questions

Below is the code that I'm using in ms access to list the data
How to write below data in below table output format ? list of dict
I am trying to make a slide toggle query. I have below list, and
How to convert list of dict to dict. Below is the list of dict
I'm trying to save data from HTML table to database using jquery to call
I am trying to parse text using pyparsing. My function is shown below. Firstly,
I have a dictionaries that look like this: s_dates_dict = {17: datetime.date(2009,21,9,0,24), 19: datetime.datetime(2011,12,1,19,39,16),
I want to do the below list iteration in django templates: foo = ['foo',
I tried the following code in LINQPad and got the results given below: List<string>
I have below a list of text, it is from a popular online game

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.