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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:25:36+00:00 2026-06-04T08:25:36+00:00

Suppose I want to create a dictionary that maps digits to numbers less than

  • 0

Suppose I want to create a dictionary that maps digits to numbers less than 100 ending in those digits as follows:

d = {}
for i in range(100):
 r = i % 10
 if r in d:
  d[r] = d[r].append(i)
 else:
  d[r] = [i]
print d

First of all, when i is 20, d[r] is apparently a NoneType when I try to append to it, throwing an error. Why would this be? Secondly, I feel like my approach is inefficient, as the work in checking if r in d isn’t propagated. Something like this would be better, I feel:

case(d[r]) of 
   SOME(L) => d[r] = L.append(i)
 | NONE => d[r] = [i]

Is there a way to have that logic in python?

  • 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-04T08:25:38+00:00Added an answer on June 4, 2026 at 8:25 am

    First of all, when i is 20, d[r] is apparently a NoneType when I try to append to it, throwing an error. Why would this be?

    This is because the following code is wrong:

    d[r] = d[r].append(i)
    

    .append modifies the list as a side effect, and returns None. So after the list is appended to, it gets thrown away and replaced with the None value now being re-assigned into d[r].

    Is there a way to have that logic in python?

    There are a variety of hacks that can be used, but none of them are appropriate here.

    Instead, solve the specific problem: “modify a dictionary value if present, or create a new value otherwise”. This can be refined into “create an empty default value if absent, and then modify the value now guaranteed to be present”.

    You can do that using .setdefault, or more elegantly, you can replace the dictionary with a collections.defaultdict:

    from collections import defaultdict
    
    d = defaultdict(list)
    for i in range(100):
        r = i % 10
        d[r].append(i)
    

    Or you can solve the even more specific problem: “create a dictionary with a given pattern”, i.e. from applying a rule or formula to an input sequence (in this case, the input is range(100):

    from itertools import groupby
    
    def last_digit(i): return i % 10
    d = {k: list(v) for k, v in groupby(sorted(range(100), key=last_digit), last_digit)}
    

    Or you can solve the even more specific problem, by taking advantage of the fact that range takes another argument to specify a step size:

    d = {i: range(i, 100, 10) for i in range(10)}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to create trees of a certain set depth, that is, the
Suppose I want to create, in the spirit of /dev/zero, a file /dev/seven that
Suppose I want to create a set of observers based on type. That is
Suppose I want to create a control in Java that is like the Symbol
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose I want to create a custom FTP server, that is: It will look
Suppose I’m making an Objective-C class that represents a fraction, and want to create
I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to put objects that identify a server into a stl set
Suppose we want two constructors for a class representing complex numbers: Complex (double re,

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.