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


Messages In This Thread
Manipulating index value, what is wrong with this code? - by Emun - Feb-05-2020, 06:55 AM

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 1,399 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 2,273 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 3,498 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 2,573 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,843 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 5,408 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 2,242 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 2,235 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 4,327 Jun-09-2021, 10:01 AM
Last Post: topfox
  How to resolve Index Error in my code? codify110 6 4,569 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