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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:35:53+00:00 2026-05-31T02:35:53+00:00

How would you add a constant number, say 1, to a value in a

  • 0

How would you add a constant number, say 1, to a value in a dictionary if certain conditions are fulfilled.

For example, if I had a dictionary:

dict = {'0':3, '1':3, '2':4, '3':4, '4':4}

If I simply wanted to add the integer 1 to every value in the dictionary so it updates dict as this:

dict = {'0':4, '1':4, '2':5, '3':5, '4':5}

When I used the following code where the Cur_FID is the first one in the dictionary ‘0’, it gave me a value of 5? It should have given me 4. ??

for lucodes in gridList2:   # a list of the values [3,3,4,4,4] -- have to separate out because it's part of a larger nested list
    if lucodes > 1:
        if lucodes < 5:
            FID_GC_dict[Cur_FID] = lucodes + 1

print FID_GC_dict[Cur_FID]   #returned 5??? weird

I want to add 1 to all the values, but stopped here when the first dictionary update did something weird.

  • 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-31T02:35:54+00:00Added an answer on May 31, 2026 at 2:35 am

    One simple way to do this is to use a collections.Counter object, which you can use in every way like a normal dictionary in most ways but it is optimized for keeping a count of items:

    >>> from collections import Counter
    >>> d = Counter({'0':3, '1':3, '2':4, '3':4, '4':4})
    >>> d
    Counter({'3': 4, '2': 4, '4': 4, '1': 3, '0': 3})
    >>> d.update(d.keys())
    >>> d
    Counter({'3': 5, '2': 5, '4': 5, '1': 4, '0': 4})
    

    As for only doing it when certain conditions are fulfilled, just use a comprehension or generator to only pass the list of the keys you want to increment to d.update():

    >>> d = Counter({'3': 4, '2': 4, '4': 4, '1': 3, '0': 3})
    >>> d.update((k for k, v in d.items() if v == 4))
    >>> d
    Counter({'3': 5, '2': 5, '4': 5, '1': 3, '0': 3})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to stringify a number and add zero-padding, like how printf(%05d) would add
How would I add key value pairs to the body of a NSMutableURLRequest? I
How would I add together the values pulled from two separate jQuery.html() calls? Example:
I would like to add some constant keys for my application, these constants can
I would like to add a value object to a mapped class where one
Background Add a constant value to numbers matched with a regular expression, using vim
I want to add a constant value onto an incoming bound integer. In fact
I'd like to write a gmail extension that would add a button on the
I'm using Elmah with ASP.NET and wondering how I would add custom data, such
I thought every time you do a flash[:notice]=Message it would add it to the

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.