In which list the value will be appended ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In which list the value will be appended ?

l = [1, 2] l = [1, 2] l.append(5) print(l) Here I have created two list of the same name. Both has different address. So in which list will the appended value is present ?

16th Aug 2022, 5:38 PM
Levi
Levi - avatar
11 Answers
+ 3
Justice "They're the same object since the have the same name. If they were different, you could have printed them both to see the difference." I thought the same way at first, but after seeing Levi 's example, it seems Python created a new list for the second declaration. To answer Levi, it is appended to the second list, as you found out in your example. There is no way to access the first list because the variable 'l' does not point to it anymore.
16th Aug 2022, 10:49 PM
Bob_Li
Bob_Li - avatar
+ 3
They're the same object since the have the same name. If they were different, you could have printed them both to see the difference.
16th Aug 2022, 5:52 PM
Justice
Justice - avatar
+ 3
Levi Well, it looks like you answered your own question within the code!
16th Aug 2022, 7:51 PM
Justice
Justice - avatar
+ 2
Q1. Is ID change relate to mutable object? Q2. Where is the old object? Still there but without the name pointer? Or in garbage? There should be method to recall it with ID if it is not clear. No matter what, the append is apply to newly declare one without doubt.
16th Aug 2022, 11:02 PM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
+ 2
abpatrick catkilltsoi Q1. yes, it appears to be. id does not change for strings and tuples Q2.Is there a way to call objects by id in Python? I would like to know. But yes, it is probably garbage collected.
16th Aug 2022, 11:30 PM
Bob_Li
Bob_Li - avatar
+ 2
import ctypes a = "hello world" print ctypes.cast(id(a),ctypes.py_object).value import gc def objects_by_id(id_): for obj in gc.get_objects(): if id(obj) == id_: return obj raise Exception("No found")
16th Aug 2022, 11:38 PM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
+ 2
#access object by id() import ctypes a = "hello world" a_adr = id(a) # storing first id print(id(a), a) # re-assigning a with same string a = "hello world" print(id(a), a) # id was not changed # re-assigning a with different string a = "hi world" a2_adr = id(a) #storing 2nd id print(id(a), a) # id is different #using ctypes to access first id print('access by id:') print(a_adr, ':', end=' ') print(ctypes.cast(a_adr,ctypes.py_object).value) print(a2_adr, ':', end=' ') print(ctypes.cast(a2_adr,ctypes.py_object).value) #or just use different variable names and avoid all of this crazy stuff...😁
16th Aug 2022, 11:51 PM
Bob_Li
Bob_Li - avatar
+ 1
Levi Do you have the Code Bit of where you have done so? Cause I can't figure out a way to make it so that print(id(l)) would know which one is which since it is the same name.
16th Aug 2022, 6:52 PM
Justice
Justice - avatar
0
Tough question : how to assign different instance in different address with the same name and same value?🤔🤔
16th Aug 2022, 6:26 PM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
0
Justice But if I print the Id of both the list they are different.
16th Aug 2022, 6:49 PM
Levi
Levi - avatar