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
#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


Messages In This Thread
RE: output list reducing each time through loop - by buran - Mar-19-2019, 11:56 AM

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