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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:05:35+00:00 2026-05-23T09:05:35+00:00

If I have a dictionary as follows (with some lists): units = [‘a’,’b’] nums

  • 0

If I have a dictionary as follows (with some lists):

units = ['a','b']
nums = ['1','2']
ratios = ['alpha', 'beta']

d = {'a_1_alpha':4, 'a_1_beta' :1, 'a_2_alpha' :2, 'a_2_beta': 3, 'b_1_alpha':2}

How do i from a new dictionary which:

  • forms a key comprising of tuple (num,ratio) #items from list nums & ratios
  • the value would be the sum of the earlier dictionary (d) value.

i.e.

new_d = { ('1','alpha'): 6, ('1','beta'): 1, ('2','alpha'): 2, ('2','beta'): 3}

I have the following code, but doesn’t seem right.

new_d = {}
for num in nums:
    for ratio in ratios:
        for k,v in d.items():
            if ratio in k:
                try:
                    oldval = dict[num,ratio]
                except:
                    oldval = 0
                new_d[(num,ratio)] = oldval + v

for p,q in new_d.items():
    print p,q

Please help to comment/advice.
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-05-23T09:05:35+00:00Added an answer on May 23, 2026 at 9:05 am

    The two outer loops are redundant, simply iterate over the key-value pairs of d. You can easily extract the three components of a key using split(). Here’s the code:

    new_d = {}
    for k, v in d.items():
       u, n, r = k.split('_')
       new_d[(n, r)] = v + new_d.get((n, r), 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some deeply randomly nested dictionary as follows. {'CompilationStatistics': {'CodeGeneration': {'EndTime': '2010-04-21T14:03:11', 'StartTime':
I have a dictionary as follows: dictA = { ('unit1','test1') : 'alpha' , ('unit1','test2')
I have a dictionary as follows: IDictionary<string, string> dict; How to create an enumerator
I have a 2D dictionary as follows dict = {'182.12.17.50': {'185.23.15.69': 30, '175.12.13.14': 14},
I have a class that inherits from a generic dictionary as follows: Class myClass
I have a file (dictionary.txt) with data field entries as follows - ABC This
I have a dictionary in Python, that describes some kinds of binaries running on
I have a dictionary as follows: {'aapl': 4.0, 'abandon': 4.0, 'absenc': 3.0, 'accept': 1.0,
I want to have a Dictionary that maps strings to generic lists of varying
I'm refactoring some code and I have written a method that modifies a Dictionary

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.