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

I am new to Python, and currently I am practicing around a bit with lists. One of the assignments is to create a 3D list. Below is the code I use to create this 3D lists. I consists of some for-loops that create a list of lists. The output after the first print statement looks valid. If I request the value of a single element of the list, say lst[1][1][1], then it gives me a single value of 0 which is what I would expect. However, when I assign the entry lst[1][1][1] a different value, lst[1][1][1] = 4, it not only updates the element in lst[1][1][1], but it updates multiple entries of the list. I would expect that it only assigns the entry lst[1][1][1] the value 4, while leaving the other entries unaffected. What am I missing here?

PS. I know that there are more elegant methods to create a 3D list (list comprehension), but I am more interested in the behavior of lists than an elegant solution.

# define variables for dimensions
a = 3
b = 5
c = 8
 
# create two lists with zeros
lst_dim2 = [0]*b
lst_dim3 = [0]*c

# create 3D list
lst = [0]*a
for i in range(a):
    lst[i] = lst_dim2 
    for j in range(b):
        lst[i][j] = lst_dim3

# print the list
print(lst)

# assign value to element of list
lst[1][1][1] = 4

# print list after element assignment
print(lst)
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
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,564 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,070 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,807 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  List of lists - merge sublists with common elements medatib531 1 3,355 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 10,073 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,162 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How do i make a new lists out of an list ozezn1 1 1,664 Oct-28-2020, 10:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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