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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:35+00:00 2026-05-31T05:17:35+00:00

def make_set(rad): y = [] for i in range(0, rad): x = [] for

  • 0
def make_set(rad):
    y = []
    for i in range(0, rad):
        x = []
        for j in range(0, rad):
            x.append(0)
        y.append(x)
    return y

def make_set_basic(rad):
    z = [[0] * rad] * rad
    return z

What is problematic about the latter implementation (make_set_basic)?

  • 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-31T05:17:36+00:00Added an answer on May 31, 2026 at 5:17 am

    When making lists using the multiplication operator, you have to take into account if the object being created is mutable or inmutable.

    In python integers are inmutables, after an arithmetic operation, a new object is created:

    >>> l = [0] * 3
    >>> l
    [0, 0, 0]
    >>> map(id, l)
    [42383312, 42383312, 42383312]
    >>> l[0] += 1
    >>> l
    [1, 0, 0]
    >>> map(id, l)
    [42383288, 42383312, 42383312]
    

    In the example above, the list has three elements all of them pointing to the same object (this is checked using the id builtin). However, when the first element is modified, this element now points to another different object and the change doesn’t affect the other elements in the list.

    On the other hand, if objects are mutable, when you use a method that modifies the object in place, the result that you get is as if the modification were applied to all objects in the list:

    >>> l = [[0]] * 3
    >>> l
    [[0], [0], [0]]
    >>> map(id, l)
    [48544944, 48544944, 48544944]
    >>> l[0].append(0)
    >>> l
    [[0, 0], [0, 0], [0, 0]]
    >>> map(id, l)
    [48544944, 48544944, 48544944]
    

    Note that in this example, after callin the append method, no new object has been created as in the previous example. Given that all the elements in the list still point to the same object (the id builtin returns the same value for all of them), the new state of the object is displayed for all the elements in the list.

    Hence, answering your question, since you probably don’t want this behaviour, you need to create the list that you need in a nested loop to make sure that all elements in the list point to a different object that you can modify without changing the state of other elements in the list as a side effect.

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

Sidebar

Related Questions

def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
# in views.py def start(request): initial_dictionary = {abc:abc} request.session['123'] = xyz return initial_dictionary def
I have two models with managers: Category and Project. class PopulatedCategoriesManager(models.Manager): def get_query_set(self): return
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
Let's say I want to make a little wrapper along the lines of: def
def foo (): x = re.compile('^abc') foo2(x) def foo2(x): How do I get x
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then

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.