Python Forum
Manipulating index value, what is wrong with this code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating index value, what is wrong with this code?
#1
I want to manipulate an index value on list1, but can't figure out why the manipulation('alt') appears at two locations instead of just one. list2 shows correctly what I want to do on list1.




list1 = []
list2 = [[[40], 70], [[[40], 70], 90]]


def append_values():

    amount = None

    if len(list1) == 0:
        amount = 70
        list1.append([[40],amount])
    else:
        amount = 90
        list1.append([list1[-1],amount])
    


def alter_lists():

    print('ori: ', list1) #Orginal list
    list1[1][0][1] = "alt"
    print('alt: ', list1)   #Altered list

    print('\n')

    print('ori: ', list2) #Orginal list
    list2[1][0][1] = 'alt'
    print('alt: ', list2)  #Altered list


append_values()
append_values()
alter_lists()
Output:

ori:  [[[40], 70], [[[40], 70], 90]]
alt:  [[[40], 'alt'], [[[40], 'alt'], 90]]


ori:  [[[40], 70], [[[40], 70], 90]]
alt:  [[[40], 70], [[[40], 'alt'], 90]]
Reply
#2
There is nothing wrong, it's as it should be.

In [1]: lst = []                                                          

In [2]: lst.append([1,2])                                                 

In [3]: lst                                                               
Out[3]: [[1, 2]]

In [4]: id(lst[0])                                                        
Out[4]: 4797778952

In [5]: lst.append(lst[0])             # append same item second time                                          

In [6]: lst                                                               
Out[6]: [[1, 2], [1, 2]]

In [7]: id(lst[1])                     # both items  refer to same object                                               
Out[7]: 4797778952

In [8]: lst[0].append(3)               # changing the object                                              

In [9]: lst                                                               
Out[9]: [[1, 2, 3], [1, 2, 3]]         # changes both as the reference is to the same object

In [10]: id(lst[0]) == id(lst[1])                                         
Out[10]: True
If this behaviour is not desired you can append new object:

In [11]: lst.append([1, 2, 3])                     # append new list                                     

In [12]: lst                                                              
Out[12]: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]         # not visually distinguishable from other items in list

In [13]: id(lst[2])                                                       
Out[13]: 4796667592                                # not same object (different id)

In [14]: id(lst[0]) == id(lst[2])                                         
Out[14]: False

In [15]: lst[2].append(4)                          # appending to new item                                                  

In [16]: lst                                                              
Out[16]: [[1, 2, 3], [1, 2, 3], [1, 2, 3, 4]]      # appends only to new object
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,469 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,179 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,680 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,486 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,601 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,428 Jun-09-2021, 10:01 AM
Last Post: topfox
  How to resolve Index Error in my code? codify110 6 2,956 May-22-2021, 11:04 AM
Last Post: supuflounder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020