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

  • Home
  • SEARCH
  • 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 8462191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:05:58+00:00 2026-06-10T14:05:58+00:00

If not immediately obvious, i’m a newbie learning via net tutorials. I’m trying to

  • 0

If not immediately obvious, i’m a newbie learning via net tutorials.

I’m trying to loop through a dict of dicts with varying lengths, and put the results in a table. I’d like to put’nothing’ into the table where an empty value might be.

I’m trying the following code:

import os

os.system("clear")

dict1 = {'foo': {0:'a', 1:'b', 3:'c'}, 'bar': {0:'l', 1:'m', 2:'n'}, 'baz': {0:'x', 1:'y'} }
list1 = []
list2 = []
list3 = []

for thing in dict1:
    list1.append(dict1[thing][0])
print list1

for thing in dict1:
    list2.append(dict1[thing][1])
print list2

for thing in dict1:
    if dict1[thing][2] == None:
        list3.append('Nothing')
    else:
        list3.append(dict1[thing][2])

and I get the following output/error:

['x', 'a', 'l']
['y', 'b', 'm']
Traceback (most recent call last):
  File "county.py", line 19, in <module>
    if dict1[thing][2] == None:
KeyError: 2

How do I refer to an empty value in a dict?

Thanks!

  • 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-10T14:06:00+00:00Added an answer on June 10, 2026 at 2:06 pm

    Use get(). The default will return a None

    val = dict1[thing].get(2)
    

    Or to specify what you want the default to be:

    val = dict1[thing].get(2, 'nothing')
    

    This way, regardless of whether the key exists, you will be able to get your valid “nothing” as a fallback.

    for thing in dict1:
        list3.append(dict1[thing].get(2, 'Nothing'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below compiles, but immediately crashes for reasons obvious to others, but not
I'm having difficulty with pages that does not show the updated data immediately Here's
Is there any reason for my jQuery effects not to be taking effect immediately?
Not quite sure how to go about describing what it is I'm trying to
I'm trying to remove a specific item from the ASP.NET Cache in order to
I have an older app that does not use ARC. When trying to address
Javascript newbie so I'm guessing this is obvious, but I'm clearly missing something fundamental
Two issues that will likely be immediately obvious, but I've spent too long already
I know it's not executed immediatly, but then when?
Not ever sure if it can be done, but is it possible to use

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.