Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference in list output
#3
Thanks a lot Thumbs Up

Okay, so we can say that a1, a2, a3 and b1 have 1 common point: they all build a list of lists.

For a1, a2 & a3, the items that make up their list of lists all point to the original list.

For exemple:
tt = [0,1] 
a1 = [tt]*4
a2 = [tt for _ in range(4)]
a1[0][1] = 6
#Or
a2[0][1] = 6
print(tt)
Output:
[0, 6]
The elements that make up the b1 list of lists all point to themselves in the original list.

For exemple:
b1 = [[0,1] for _ in range(4)]
b1[0][1] = 6
cc = b1[0]
print(cc)
print(b1)
Output:
[0, 6] [[0, 6], [0, 1], [0, 1], [0, 1]]
cc[1] = 2
print(cc)
print(b1)
Output:
[0, 2] [[0, 2], [0, 1], [0, 1], [0, 1]]
tt is assigned by a list. So for a1 & a2 we copy n times (by multilication and iteration) into a list some references of the list tt.

For a3 the sub-list will be assigned in literal form. In a list, some references from this literal list will be simply copied n times (by multiplication) for a result similar to a1 & a2.

For b1 the sub-list will be assigned in literal form and re-evaluated for each copy (by iteration) in a list for n independent copy.

So for a2 =[tt for _ in range(4)], tt is not re-evaluated because it refers to the list assigned to it, unlike b1 = [[0,1] for _ in range(4)].
For a1 & a3 it is a simple copy, from a list assigned for a1 and from a list expressed literally for a3.

Is it correct?
Reply


Messages In This Thread
Difference in list output - by OokaydO - Nov-05-2019, 08:44 PM
RE: Difference in list output - by ichabod801 - Nov-05-2019, 09:14 PM
RE: Difference in list output - by OokaydO - Nov-08-2019, 10:20 AM
RE: Difference in list output - by perfringo - Nov-08-2019, 01:18 PM
RE: Difference in list output - by OokaydO - Nov-08-2019, 02:48 PM
RE: Difference in list output - by ichabod801 - Nov-08-2019, 03:21 PM
RE: Difference in list output - by OokaydO - Nov-09-2019, 12:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,155 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Output difference from 2 lists of different sizes with words gracenz 5 1,391 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  set.difference of two list gives empty result wardancer84 4 1,571 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  sum() list from SQLAlchemy output Personne 5 4,613 May-17-2022, 12:25 AM
Last Post: Personne
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,276 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  How to append to list a function output? rama27 5 6,856 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 5,365 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,523 Feb-19-2020, 09:17 AM
Last Post: kroh
  output list reducing each time through loop 3Pinter 6 3,596 Mar-19-2019, 01:31 PM
Last Post: perfringo
  os.popen output to a list .. evilcode1 1 5,373 Oct-02-2018, 08:42 PM
Last Post: ODIS

Forum Jump:

User Panel Messages

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