Python Forum
output list reducing each time through loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output list reducing each time through loop
#1
In getting empty lists as output, and that is something I don't get.

Expected:
[1,2,3,4,5]
[2,3,4,5]
[3,4,5]
[4,5]
[5]

a = [1,2,3,4,5]
output = []

while a:
  output.append(a)
  a.pop(0)

print(output)
I want a seperate 'output'container so I can use that afterwards.
Reply
#2
You are getting list of empty lists, not empty list
Output:
[[], [], [], [], []] >>>
Check this link https://nedbatchelder.com/text/names.html
lists are mutable. when you append a to output on line 5, every list you append refers to same elements. when you pop element on line 6 you pop it out from all the lists. To fix this, you need to append copy of a to output
a = [1,2,3,4,5]
output = []
 
while a:
  output.append(a[:])
  a.pop(0)
 
print(output)
Output:
[[1, 2, 3, 4, 5], [2, 3, 4, 5], [3, 4, 5], [4, 5], [5]] >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks Buran,

I think it makes sense to me, but I'll read the link you posted so I can fully understand it! Food for thought!

3Pinter
Reply
#4
Hey Buran,

Super interesting article and it explained a lot to me. However I don't get a tiny thing: lists are mutable, okay. And therefor doing this:

start = [1,2,3,4,5]
new = []
t = True
for s in start:
    new.append(start)
    if t:
    	start.pop(0)
        t = False
print(new)
"new" will output [2,3,4,5] a few times. As expected.

But since lists are mutable I would expect that changing "start" to a new definition would alter the "new" list as well.

start = [1,2,3,4,5]
new = []
t = True
for s in start:
    new.append(start)
    if t:
    	start.pop(0)
        t = False
start = ["test"]
print(new)
But python will still output [2,3,4,5] a few times.

Why doesn't it output "test" a few times?
Reply
#5
on line 9 you don't mutate start, you create new list, i.e. start now points to different list:

start = [1,2,3,4,5]
print(f'id of start: {id(start)}')
new = []
print(f'id of new: {id(new)}')
t = True
for s in start:
    new.append(start)
    if t:
        start.pop(0)
        t = False
start = ['test']
print(f'id of start: {id(start)}')
print(new)
print(f'id of new: {id(new)}')
Output:
id of start: 4088456 id of new: 32132424 id of start: 32124040 [[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]] id of new: 32132424 >>>
as you can see the id of start changes, while the id of new is the same


Now, here is what you were looking/expecting:

start = [1,2,3,4,5]
print(f'id of start: {id(start)}')
new = []
print(f'id of new: {id(new)}')
t = True
for s in start:
    new.append(start)
    if t:
        start.pop(0)
        t = False
print(new)
start[0] = 'test'
print(f'id of start: {id(start)}')
print(new)
print(f'id of new: {id(new)}')
Output:
id of start: 4153992 id of new: 31997384 [[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]] id of start: 4153992 [['test', 3, 4, 5], ['test', 3, 4, 5], ['test', 3, 4, 5], ['test', 3, 4, 5]] id of new: 31997384 >>>
here you mutate the original start (id is not changed) and you can see that change is also reflected in new
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
ah, python too has id's. that explains it all.

Thanks again Buran for your explanation!
Reply
#7
Additional tidbit: if you really after "output 'test' few times" then it can be done this way:

start[:] = ['test']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sum() list from SQLAlchemy output Personne 5 4,345 May-17-2022, 12:25 AM
Last Post: Personne
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,175 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Real-Time output of server script on a client script. throwaway34 2 2,010 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 45,823 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  How to measure execution time of a multithread loop spacedog 2 2,836 Apr-24-2021, 07:52 AM
Last Post: spacedog
  Time.sleep: stop appending item to the list if time is early quest 0 1,847 Apr-13-2021, 11:44 AM
Last Post: quest
  concatenating 2 items at a time in a python list K11 3 2,246 Oct-21-2020, 09:34 AM
Last Post: buran
  fraction module: can you stop the reducing? qmfoam 1 2,350 Oct-10-2020, 06:10 PM
Last Post: bowlofred
  using 'while loop' output going into infinite loop... amitkb 2 1,910 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Appending list Trouble Big Time Milfredo 7 3,023 Oct-01-2020, 02:59 AM
Last Post: Milfredo

Forum Jump:

User Panel Messages

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