Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference in list output
#2
Lists are mutable. So if you reassign one, it doesn't reassign the whole list, it doesn't make a copy of one. You just now have two names pointing to the same list. And if you make a list of lists (if you're not careful), you end up with a list of references to the same list.

So the first two lists you have, a1 and a2, and just a bunch of references to the same list that tt is pointing at. If you change any one of them, that changes the list they are all pointing at. Now you can look at any of them, and they are all pointing at the original list which you changed.

a3 is similar, in that it's one list. The multiplication just makes more references to that same list. It's a different list than tt, but a3 is still four things pointing to the same list.

b1 is different because each time through the loop in the list comprehension, the list is reevaluated. So it is four pointers to four different lists. They are still pointers, though. So if you assigned cc = b[0], and changed cc, b[0] would also reflect those changes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
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,218 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Output difference from 2 lists of different sizes with words gracenz 5 1,435 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  set.difference of two list gives empty result wardancer84 4 1,604 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  sum() list from SQLAlchemy output Personne 5 4,700 May-17-2022, 12:25 AM
Last Post: Personne
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,310 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  How to append to list a function output? rama27 5 6,918 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 5,417 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,532 Feb-19-2020, 09:17 AM
Last Post: kroh
  output list reducing each time through loop 3Pinter 6 3,627 Mar-19-2019, 01:31 PM
Last Post: perfringo
  os.popen output to a list .. evilcode1 1 5,411 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