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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:59:00+00:00 2026-06-15T18:59:00+00:00

I have a dictionary and I want to insert keys and values dynamically but

  • 0

I have a dictionary and I want to insert keys and values dynamically but I didn’t manage to do it. The problem is that when I use the update method it doesn’t add a pair but it deletes the previous values so I have only the last value when printing the dictionary
here is my code

i = 0
for o in iterload(f):
    i=i+1
    mydic = {i : o["name"]}
    mydic.update({i : o["name"]})
    for k, v in mydic.items():
        print(k,v) 
print(mydic)

f is a file that i’m parsing with python code
as a result I get

{3: 'toto'}

which is the last element. is there a solution to have all the elements in my dictionary

Thanks in advance

I have another question

Now I need to chek if an input value equals a key from my dictionary and if so I need to get the value of this key to continue parsing the file and get other informations.

Here is my code :

f = open('myfile','r')
nb_name = input("\nChoose the number of the name :")

for o in iterload(f):
    if o["name"] == mydic[nb_name]: 
        ...

I get a keyError

Traceback (most recent call last):
  File ".../test.py", line 37, in <module>
            if o["name"] == mydic[nb_name]: 
KeyError: '1'

I don’t understand 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-15T18:59:01+00:00Added an answer on June 15, 2026 at 6:59 pm

    Remove the following line:

        mydic = {i : o["name"]}
    

    and add the following before your loop:

    mydic = {}
    

    Otherwise you’re creating a brand new one-element dictionary on every iteration.

    Also, the following:

    mydic.update({i : o["name"]})
    

    is more concisely written as

    mydic[i] = o["name"]
    

    Finally, note that the entire loop can be rewritten as a dictionary comprehension:

    mydic = {i+1:o["name"] for i,o in enumerate(iterload(f))}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary where I want to alphabetically sort the keys that have
I have class myCollection that inherit from Dictionary. Want to hide the add method
Hi want to have a ordered dictionary with keys having list of values. from
I have a dictionary that I want to add all the values to an
I have a dictionary of strings that i want the user to be able
I have a dictionary with 3 values associated to each key, I want to
I have a Dictionary<string, List<Order>> and I want to have the list of keys
I have a dictionary that I want to show in a form. What's the
I have a dictionary of type Dictionary<int, double> and want to eliminate values equal
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want

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.