Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
behavior list of lists
#3
Hi Yoriz,

thanks for the reply. Oke, I see. So in your first example (which corresponds to my case), the values are changed in multiple places because the lists are the same, right?

Now if I consider the following example:

# define variable
a = 0

# create 1D list
lst = [0]*10
for i in range(10):
    lst[i] = a
 
# print the list
print(lst)
 
# assign value to element of list
lst[1] = 4
 
# print list after element assignment
print(lst)
I would expect that changing lst[1] results in a change of all entries in that list, because in this case lst[0] == lst[1] == lst[2] etc.. Why does it not update the entire list in this case?
Reply


Messages In This Thread
behavior list of lists - by roym - Jul-04-2021, 10:24 AM
RE: behavior list of lists - by Yoriz - Jul-04-2021, 11:20 AM
RE: behavior list of lists - by roym - Jul-04-2021, 12:34 PM
RE: behavior list of lists - by Yoriz - Jul-04-2021, 01:30 PM
RE: behavior list of lists - by deanhystad - Jul-04-2021, 02:20 PM
RE: behavior list of lists - by roym - Jul-04-2021, 04:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 1,746 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  List all possibilities of a nested-list by flattened lists sparkt 1 1,854 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 2,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 2,082 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 3,319 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 3,403 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 2,964 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  List of lists - merge sublists with common elements medatib531 1 4,220 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 15,880 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,846 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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