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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:15:46+00:00 2026-05-30T18:15:46+00:00

I am using Django to build a site mainly to learn something about websie

  • 0

I am using Django to build a site mainly to learn something about websie building.And I came across something I thought is strange.I have this code below:

all_words=Word.objects.all()[0:100]
    user=request.user
    wordlist = []
    //wordinfo = {} --->This doesn't work
    for word in all_words:
        wordinfo = {}//This works fine
        taged_word = FlagWord.objects.filter(word = word,user = user)
        if taged_word :
            wordinfo['usertag'] = True
        else:
            wordinfo['usertag'] = False
        wordinfo['word'] = word
        wordlist.append(wordinfo)

Notice where the wordinfo is placed.I think both would work because the latter content would replace the previous one anyway.But when it’s placed outside the for loop,I would get 100 elements in the wordlist which are all the same.The word property would all be the last word in all_words.

I know if the wordinfo is placed in the for loop,a new wordinfo would be created.But question is I think if it’s placed outside the for loop,it should also work.Can somebody explain to me what’s the difference?Why can’t it be placed outside the for loop?

  • 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-30T18:15:47+00:00Added an answer on May 30, 2026 at 6:15 pm

    in python variables reference objects. so when wordinfo is outside of the loop the values it contains are referenced by your list of values.

    This means that on your last itearation all the values in wordlist will be equal to the last value assigned to wordinfo

    so at the end of the loop you will have

    wordlist = [wordinfo, wordinfo, wordinfo, wordinfo] # 100 times
    

    if wordinfo is eaual to {'usertag': True, 'word': 'because'} all the items in the list will have that value.

    you could further refactor this to look something like:

      all_words=Word.objects.all()[0:100]
      user=request.user
      wordlist = []
      for word in all_words:
        taged_word = FlagWord.objects.filter(word = word,user = user)
        usertag = False
        if taged_word :
            usertag = True
        wordlist.append({'word': word, 'usertag': usertag)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a real estate site in Django and have a Home model, which
I have a Django site where a strange bug is occurring. On the site
I have built a site using Django and I am receiving this annoying error
I want to build an API service using Django. A basic workflow goes like
I'm willing to build a restful service using Django, I'm coming form RoR background
I am trying to build a web site in both English and Bulgarian using
I have been using django to write a rather complicated object-oriented model for a
I have chosen Python as a langauge to build my ecommerce webiste. The site
I'm using django-piston for my REST json api, and I have it all set
I'm going to build a website using OSQA which is built on Django (a

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.