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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:02:11+00:00 2026-06-05T17:02:11+00:00

This program builds a dictionary out of a list based on what two index

  • 0

This program builds a dictionary out of a list based on what two index numbers you would like from the list.
this is the output

{‘a’: 2, ‘c’: 7, ‘b’: ‘h’}

def buildDict2(theList,inputOne,inputTwo) :
    newdict={}
    for a in theList:
        if inputTwo >= len(a):
                print "error"
                return "error"
        if inputOne >= len(a):
                print "error"
                return "error"
        for oneValue in a: 
                    print len(a)

                    varkey=a[inputOne]
                    varvalue=a[inputTwo]
                    newdict[varkey]=varvalue


    return newdict
print buildDict2([["a", "s","d", 2,0,1],["b", "f",3,"h",0,2],["c", "g",5,7]],0,3)

How do i convert this to a single loop?
+1 all answers
thank you for your help, it is greatly appreciated.

basically i just need to use one loop instead of two

edit: important concept: it takes a dictionary and two numbers. the two numbers tell the program what index of the lists to use. list=[[a,b,c],[d,e,f],[h,g,i]] used with the numbers 0 and 2 will return a dictionary with three entries. the keys will be the first input. so the keys are a d and h, and the values are going to be the list index of second input, so c, e i. get it?

for this code

def buildDict2(theList, range1, range2, newdict = {}):
    for a in theList:
        if range2 >= len(a) or range1 >= len(a):
            return "error"
        newdict[a[range1]] = a[range2]
        return newdict
print buildDict2([["a", "s","d", 2,0,1],["b", "f",3,"h",0,2],["c", "g",5,7]],0,3)

i am getting {‘a’: 2}
when i want to get
{‘a’: 2, ‘c’: 7, ‘b’: ‘h’}
whats the problem?

  • 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-05T17:02:14+00:00Added an answer on June 5, 2026 at 5:02 pm
    def buildDict2(theList, range1, range2):
        newdict = {}
        for a in theList:
            if range2 >= len(a) or range1 >= len(a):
                return "error"
            newdict[a[range1]] = a[range2]
        return newdict
    if __name__ == '__main__':
        print buildDict2([["a", "s","d", 2,0,1],["b", "f",3,"h",0,2],["c", "g",5,7]],0,3)
    

    changed to pep8 indentation as per Jakob’s recommendation so you can see the indentation more clearly. you put your return statement at the wrong level.

    corrected mutable default as per astynax’s comment.

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

Sidebar

Related Questions

This program reads emails (really just a .txt file structured like an email) and
This program takes the first number in a file and indicates how many numbers
This program builds with no problem, and the executable starts up, but no triangle
I am running a program that builds a very large object, and I would
This program int main() { int a, b; a = b; return 0; }
This program should check whether the value of getTotalCount() is greater thana a number
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program I'm doing is about a social network, which means there are users
This program is meant to generate a dynamic array, however it gives an access

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.