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

  • Home
  • SEARCH
  • 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 8219725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:16:36+00:00 2026-06-07T13:16:36+00:00

I’m trying to update values in a list of lists using the following source

  • 0

I’m trying to update values in a list of lists using the following source code:

target_agreement = get_object_or_404(TargetAgreement.objects, pk=agreement_id)
target_category_set = TargetCategory.objects.filter(company=target_agreement.company, is_active=True).order_by('position')

category_targets = []
if target_category_set:
    totals = [[0]*3]*len(target_category_set) #list of lists with list on level 2 having length of 3
else:
    totals = [[0]*3]*1

for (index1,target_category) in enumerate(target_category_set):
        category_targets_temp = []

        for (index2,target) in enumerate(target_category.category_targets.filter(user=request.user, agreement=target_agreement)):
            category_targets_temp.append(target)
            print "*******"
            print "index is: " 
            print index1
            print totals[index1][0]
            totals[index1][0] = totals[index1][0] + target.weight
            print totals[index1][0]
            print "totals are"
            print totals
            print "*******"
        print "final result"
        print totals[index1][0]
        print totals
        print "-----"
        category_targets.append(category_targets_temp)
    print totals

The behavior I do not understand is that totals[index1][0] = totals[index1][0] + target.weight is not only updating the first element in the list referenced by index1, but all first elements in all lists.

The result is like the following:

[[88, 0, 0], [88, 0, 0], [88, 0, 0], [88, 0, 0]]

But I would have expected:

[[36, 0, 0], [50, 0, 0], [1, 0, 0], [1, 0, 0]]

Can somebody clarify what I did wrong. Thanks for your help.

  • 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-07T13:16:39+00:00Added an answer on June 7, 2026 at 1:16 pm

    The reason is the way you created that totals list. By multiplying the list –

    totals = [[0]*3]*len(target_category_set) 
    

    You create a list whose elements are references to the same list. Thus, when you modify one element, all of them are modified. Consider:

    In [1]: l = [[1, 2]]*3
    
    In [2]: l
    Out[2]: [[1, 2], [1, 2], [1, 2]]
    
    In [3]: l[0].append(3)
    
    In [4]: l
    Out[4]: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    

    But you could avoid this by changing the list definition:

    In [5]: l = [[1, 2] for _ in range(3)]
    
    In [6]: l[0].append(3)
    
    In [7]: l
    Out[7]: [[1, 2, 3], [1, 2], [1, 2]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.