0

So I was deleting empty indexes in my list of

['','','','','A','B','C','','']

I used

list.pop[0]
list.pop[1]
list.pop[2]
list.pop[3]
list.pop[-1]
list.pop[-2]

in order to remove the empty objects.

But it does not delete the first 2 items and skips to [A] at list.pop[3], deleting data when it is not my intention to do so.

What's wrong and how can I get around this? Sorry for the non-Pythonic code.

Here's the exact code I used.

import requests, re
from bs4 import BeautifulSoup 

url='https://bn.mappersguild.com/'

headers={"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}

page = requests.get(url, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')

osurow = soup.find('td', string='osu')
osu = osurow.find_parent("table", {"class": "table table-sm table-dark table-hover col-6 col-md-3"})

osutext = osu.get_text() 
osu = osu.find_all('a')

osuname = osutext.split('\n')

osuname.remove('osu')

osuname.pop(0)
osuname.pop(1)
osuname.pop(2)
osuname.pop(3) #Here's the point where it deletes - Mo - 
osuname.pop(-1)
osuname.pop(-2)
osuname.pop(-3) #Here's the point where it deletes Zelq


print ('osu!standard profile listing')
for element in osu:
    print(element)

print(osuname)

c-sig
  • 15
  • 5
  • This might help you to remove empty values from list: https://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings – Amiram Apr 05 '20 at 13:16

1 Answers1

1

Let's monitor the contents of osuname while popping stuff from it:

>>> osuname = ['','','','','A','B','C','','']
>>> osuname.pop(0); osuname
''
['', '', '', 'A', 'B', 'C', '', '']
>>> osuname.pop(1); osuname
''
['', '', 'A', 'B', 'C', '', '']
>>> osuname.pop(2); osuname
'A'
['', '', 'B', 'C', '', ''] # HOLD ON!

As you pop things, the list shrinks! So the elements that are found at, say, index 2, are shifting:

# Original list
['','','','','A','B','C','','']
       ^^ <- index 2

# List after pop(0); pop(1)
['', '', 'A', 'B', 'C', '', '']
         ^^^ <- index 2

To remove empty strings from the list, filter it:

>>> osuname = ['','','','','A','B','C','','']
>>> list(filter(lambda element: element != '', osuname))
['A', 'B', 'C']
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Filtering the list seems to delete everything and leaves at it's place. I don't really understand why since replacing != with == seems to have the same outcome even though they're different operators. Is there an alternative way to this? – c-sig Apr 05 '20 at 13:44
  • That's why I used `list(filter(...))` instead of just `filter` – ForceBru Apr 05 '20 at 13:45