Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Manipulating index value, what is wrong with this code? Emun Unladen Swallow Posts: 1 Threads: 1 Joined: Feb 2020 Reputation: 0 Likes received: 0 #1 Feb-05-2020, 06:55 AM 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]] perfringo Da Bishop Posts: 1,306 Threads: 6 Joined: Jun 2018 Reputation: 131 Likes received: 284 #2 Feb-05-2020, 07:18 AM (This post was last modified: Feb-05-2020, 07:18 AM by perfringo. Edited 3 times in total.) 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. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post tell me what's wrong with my code? codingisannoyingaf 6 256 Jan-02-2020, 11:28 AM Last Post: adaomacarl What is wrong with my chemistry code? mimimycin 9 306 Dec-30-2019, 06:43 PM Last Post: mimimycin I don't know what's wrong with this code [Python Console] H0M1C1D4L_P1G 6 247 Dec-22-2019, 10:27 PM Last Post: snippsat what's wrong with my code Oaklizard2047 4 188 Nov-03-2019, 06:16 AM Last Post: Oaklizard2047 python gives wrong string length and wrong character thienson30 2 281 Oct-15-2019, 08:54 PM Last Post: Gribouillis How does the code run? My code wrong? jollydragon 0 200 Oct-10-2019, 06:24 AM Last Post: jollydragon What's wrong with this code? kumaaaa 6 373 Sep-28-2019, 12:06 PM Last Post: DeaD_EyE Wrong output in Visual Studio Code py_learner 1 336 Jun-24-2019, 10:02 PM Last Post: Yoriz elevator simulator...whats the wrong at this code? tasos710 5 1,375 Jun-11-2019, 01:38 AM Last Post: micseydel Manipulating CSV Prince_Bhatia 1 301 Apr-25-2019, 11:55 AM Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)