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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:25:11+00:00 2026-06-18T21:25:11+00:00

I am having a problem updating values in a dictionary in python. I am

  • 0

I am having a problem updating values in a dictionary in python. I am trying to update a nested value (either as an int or list) for a single fist level key, but instead i update the values, for all first level keys.

I start by creating the dictionary:

kmerdict = {}
innerdict = {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 'lowstart':0,'totaluncover':0, 'totalbases':0}
for kmer in kmerlist: # build kmerdict
    kmerdict [kmer] = {}
    for chrom in fas:  #open file and read line
        chromnum = chrom[3:-3]
        kmerdict [kmer][chromnum] = innerdict

Then i am walking through chromosomes (as plain text files) from a list (fas, not shown), and taking 7mer strings (k=7) as the key. If that key is in a list of keys i am looking for (kmerlist) and trying to use that to reference a single value nested in the dictionary:

for chrom in fas:  #open file and read line
    chromnum = chrom[3:-3]
    p = 0  #chromosome position counter
    thisfile = "/var/store/fa/" + chrom
    thischrom = open(thisfile)
    thischrom.readline()
    thisline = thischrom.readline()
    thisline = string.strip(thisline.lower())
    l=0  #line counter
    workline = thisline
    while(thisline):
        if len(workline) > k-1:
            thiskmer = ''
            thiskmer = workline[0:k] #read five bases
            if thiskmer in kmerlist:
                thisuncovered = kmerdict[thiskmer][chromnum]['uncovered']
                thisendcover =  kmerdict[thiskmer][chromnum]['endcover']
                thiscoverholder = kmerdict[thiskmer][chromnum]['coverholder']
                if p >= thisendcover:
                    thisuncovered += (p - thisendcover)
                    thisendcover = ((p+k) + ext)
                    thiscoverholder.append(p)
                elif p < thisendcover:
                    thisendcover = ((p+k) + ext)
                    thiscoverholder.append(p)
            print kmerdict[thiskmer]
            p += 1
            workline = workline[1:]
        else:
            thisline = thischrom.readline()
            thisline = string.strip(thisline.lower())
            workline = workline+thisline
            l+=1
print kmerdict

but when i print the dictionary, all “thiskmer” levels are getting updated with the same values. I’m not very good with dictionaries, and i can’t see the error of my ways, but they are profound! Can anyone enlighten me?

Hope i’ve been clear enough. I’ve been tinkering with this code for too long now 🙁

  • 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-18T21:25:12+00:00Added an answer on June 18, 2026 at 9:25 pm

    confession — I haven’t spent the time to figure out all of your code — only the first part. The first problem you have is in the setup:

    kmerdict = {}
    innerdict = {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 
                 'lowstart':0,'totaluncover':0, 'totalbases':0}
    for kmer in kmerlist: # build kmerdict
        kmerdict [kmer] = {}
        for chrom in fas:  #open file and read line
            chromnum = chrom[3:-3]
            kmerdict [kmer][chromnum] = innerdict
    

    You create innerdict once and then proceed to use the same dictionary over an over again. In other words, every kmerdict[kmer][chromnum] refers to the same objects. Perhaps changing the last line to:

    kmerdict [kmer][chromnum] = copy.deepcopy(innerdict)
    

    would help (with an appropriate import of copy at the top of your file)? Alternatively, you could just move the creation of innerdict into the inner loop as pointed out in the comments:

    def get_inner_dict():
        return {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 
                'lowstart':0,'totaluncover':0, 'totalbases':0}
    
    kmerdict = {}
    for kmer in kmerlist: # build kmerdict
        kmerdict [kmer] = {}
        for chrom in fas:  #open file and read line
            chromnum = chrom[3:-3]
            kmerdict [kmer][chromnum] = get_inner_dict()
    

    — I decided to use a function to make it easier to read :).

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

Sidebar

Related Questions

Im trying to update details of a single customer and I'm having problems updating
I am having problem updating table cell value using jQuery 1.4.2. it all works
I am trying to learn mysql and having some problem with updating/adding data to
I'm having a problem while updating cell value of the JTable. What i want
I'm having problems updating records to contain NULL values - in particular, a field
I am having problem to get a numerical value for this expression where I
I'm having a problem updating a row after an inline edit. My ColModel is:
I'm having a problem updating a table and im sure its pretty straight forward
I am having an update statament in my jsp. The problem is that when
I'm having problems updating a div's content in IE using .update; I also tried

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.