2

I am trying to create two different lists using two for loops, but it creates two identical lists. When I comment out one for loop it creates the list I am trying to get

import requests
from bs4 import BeautifulSoup

locations = dates_list = []
   
url = 'https://www.fis-ski.com/DB/general/statistics.html?statistictype=positions&positionstype=position&offset=50&sectorcode=JP&seasoncode=1980&categorycode=WC&gendercode=&competitornationcode=&place=&nationcode=&position=4&disciplinecode='
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
res = requests.get(url, headers=headers)

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

venues = soup.find_all('a', attrs={'class': 'bb-xs pb-xs-1_1 g-xs-11 g-sm-6 g-md-4 g-lg-4 justify-left bold'})
dates = soup.find_all('a', attrs={'class': 'bb-xs pb-xs-1_1 pl-xs-1 g-xs-6 g-sm-3 g-md-2 g-lg-2 justify-left'})

for venue in venues:
    locations.append(venue.get_text())
        
for date in dates:
    dates_list.append(date.get_text())

print(locations)
print(dates_list)
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Chris 789
  • 41
  • 1
  • 1
    Initialising two lists in the same line will create two references to the same object. Initialise them separately and it should work as expected. – mara004 Apr 13 '22 at 19:51

1 Answers1

-1

locations and dates_list is actually the same place in memory!

Try this code again but setting them to their own lists separately on 2 lines. This link may help you out Changing one list unexpectedly changes another, too

David Jay Brady
  • 1,034
  • 8
  • 20
  • If the majority of your answer consists of a link to another post, just flag the question as a duplicate, don't post it as an answer. – MattDMo Apr 13 '22 at 19:50
  • The two questions are a bit different. The linked post is only for further reading. I don't think this one should be classified as duplicate. – mara004 Apr 13 '22 at 19:53