AnswerBun.com

Why does this list element move back in the list?

Stack Overflow Asked by user13990775 on January 5, 2022

For context, what I am trying to do is create a top-down display in which the screen is just text which is printed every time the player gives input. What this script does is move the Y upward so that I can find things I could do to move the player when the input is given.

minimap=[
        'HHHHHHHHHHHHHHHHHHHH',
        'H                  H',
        'H                  H',
        'H           HHHHHHHH',
        'H                  H',
        'HHHHHHHH           H',
        'H                  H',
        'H          Y       H',
        'HHHHHHHHHHHHHHHHHHHH']
for li in minimap:
    print(li)
count=0
for b in minimap:
    count+=1
    if count==7:
        soi=b
    elif count==8:
        s2i=b
minimap.pop(7)
minimap.pop(6)
list(soi).pop(11)
list(s2i).pop(11)
list(soi).insert(11,' ')
list(s2i).insert(11,'Y')
minimap.insert(7,soi)
minimap.insert(6,s2i)
for li in minimap:
    print(li)

So what happens in the two lines

minimap.insert(7,soi)
minimap.insert(6,s2i)

Is that the new, changed lines are inserted into the minimap[] positions of the old lines, which were removed from minimap[] (I say this because I believe it is most relevant to the issue).

However this is what happens:

HHHHHHHHHHHHHHHHHHHH
H                  H
H                  H
H           HHHHHHHH
H                  H
HHHHHHHH           H
H                  H
H          Y       H
HHHHHHHHHHHHHHHHHHHH

(New minimap:)

HHHHHHHHHHHHHHHHHHHH
H                  H
H                  H
H           HHHHHHHH
H                  H
HHHHHHHH           H
H          Y       H
HHHHHHHHHHHHHHHHHHHH
H                  H

Everything worked as expected, however, (it seems that) line 8 and (new) line 7 swapped positions. Why did this happen?

One Answer

What happened is that when you

minimap.insert(7,soi)

There were only 6 items left in your minimap, and the 6th item is 'HHHHHHHHHHHHHHHHHHHH'.

So by inserting soi to the 7th item, you are actually putting it after the 'HHHHHHHHH'.

Instead, you would want:

minimap.insert(6,soi)
minimap.insert(6,s2i)

or

minimap.insert(6,s2i)
minimap.insert(7,soi)

Answered by Ranoiaetep on January 5, 2022

Add your own answers!

Related Questions

Update sql database from datagridview in c#

2  Asked on December 25, 2021 by user13987069

   

Wait for observable to get array-data

2  Asked on December 25, 2021 by bena

     

How can you add an object into json through python

1  Asked on December 25, 2021 by jonasmanthebot

   

Pandas reindex to fill date index

1  Asked on December 25, 2021 by niviral

       

checking if file exists in FileInfo[]

1  Asked on December 25, 2021 by abhilash-vg

     

Processing of a file: byte arrays and dimentions

2  Asked on December 25, 2021 by ld420

   

CSS Grid template areas in wrong order

1  Asked on December 25, 2021

   

c++ should I prefer union or exception

2  Asked on December 25, 2021 by chetzacoalt

   

How to set default state for an object when using useState

1  Asked on December 25, 2021 by ross-attrill

   

friend class and the scope of function arguments

2  Asked on December 25, 2021 by user2269707

   

How to remove unchecked value from list?

3  Asked on December 25, 2021 by afelip

   

handling for loop exceptions

6  Asked on December 25, 2021 by sivaram

     

How to filter JSON Data in JS

1  Asked on December 25, 2021 by user12129876

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP