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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:20:24+00:00 2026-05-19T00:20:24+00:00

Say I have two lists: >>> l1=[1,2,3,4] >>> l2=[11,12,13,14] I can put those lists

  • 0

Say I have two lists:

>>> l1=[1,2,3,4]
>>> l2=[11,12,13,14]

I can put those lists in a tuple, or dictionary, and it appears that they are all references back to the original list:

>>> t=(l1,l2)
>>> d={'l1':l1, 'l2':l2}
>>> id(l1)==id(d['l1'])==id(t[0])
True
>>> l1 is d['l1'] is t[0]
True

Since they are references, I can change l1 and the referred data in the tuple and dictionary change accordingly:

>>> l1.append(5)
>>> l1
[1, 2, 3, 4, 5]
>>> t
([1, 2, 3, 4, 5], [11, 12, 13, 14])
>>> d
{'l2': [11, 12, 13, 14], 'l1': [1, 2, 3, 4, 5]}

Including if I append the reference in the dictionary d or mutable reference in the tuple t:

>>> d['l1'].append(6)
>>> t[0].append(7)
>>> d
{'l2': [11, 12, 13, 14], 'l1': [1, 2, 3, 4, 5, 6, 7]}
>>> l1
[1, 2, 3, 4, 5, 6, 7]

If I now set l1 to a new list, the reference count for the original list decreases:

>>> sys.getrefcount(l1)
4
>>> sys.getrefcount(t[0])
4
>>> l1=['new','list']
>>> l1 is d['l1'] is t[0]
False
>>> sys.getrefcount(l1)
2
>>> sys.getrefcount(t[0])
3

And appending or changing l1 does not change d['l1'] or t[0] since it now a new reference. The notion of indirect references is covered fairly well in the Python documents but not completely.

My questions:

  1. Is a mutable object always a reference? Can you always assume that modifying it modifies the original (Unless you specifically make a copy with l2=l1[:] kind of idiom)?

  2. Can I assemble a list of all the same references in Python? ie, Some function f(l1) that returns ['l1', 'd', 't'] if those all those are referring to the same list?

  3. It is my assumption that no matter what, the data will remain valid so long as there is some reference to it.

ie:

l=[1,2,3]         # create an object of three integers and create a ref to it
l2=l              # create a reference to the same object
l=[4,5,6]         # create a new object of 3 ints; the original now referenced 
                  # by l2 is unchanged and unmoved
  • 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-19T00:20:25+00:00Added an answer on May 19, 2026 at 12:20 am

    1) Modifying a mutable object through a reference will always modify the “original”. Honestly, this is betraying a misunderstanding of references. The newer reference is just as much the “original” as is any other reference. So long as both names point to the same object, modifying the object through either name will be reflected when accessed through the other name.

    2) Not exactly like what you want. gc.get_referrers returns all references to the object.

    >>> l = [1, 2]
    >>> d = {0: l}
    >>> t = (l, )
    >>> import gc
    >>> import pprint
    >>> pprint.pprint(gc.get_referrers(l))
    [{'__builtins__': <module '__builtin__' (built-in)>,
      '__doc__': None,
      '__name__': '__main__',
      '__package__': None,
      'd': {0: [1, 2]},
      'gc': <module 'gc' (built-in)>,
      'l': [1, 2],
      'pprint': <module 'pprint' from '/usr/lib/python2.6/pprint.pyc'>,
      't': ([1, 2],)},   # This is globals()
    
     {0: [1, 2]},  # This is d
     ([1, 2],)]   # this is t
    

    Note that the actual object referenced by l is not included in the returned list because it does not contain a reference to itself. globals() is returned because that does contain a reference to the original list.

    3) If by valid, you mean “will not be garbage collected” then this is correct barring a highly unlikely bug. It would be a pretty sorry garbage collector that “stole” your data.

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

Sidebar

Related Questions

Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
I have two lists ( not java lists, you can say two columns) For
Let's say that I have got two lists: temp<-c(con.sin.results,sin.results,exp.results) Temp<-c([,1:16],[,17:32],[,33:48],[,49:64]) Each of the variables
Say you have a std::list of some class. There are two ways you can
Lets say I have a function f[x_, y_] , and two lists l1 ,
Say I have two classes and have a requirement that the primary key property
let's say I have two lists: a = list(1,2,3) b = list(4,5,6) So I
Say I have two lists or arrays of strings. For example: list 1: a,
Say I have two lists val L1 = List[(Int, Int)]((1,1), (2,2)) val L2 =
Say I have two UIViewControllers , all working under a UINavigationController : RootViewController hosts

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.