Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Disctionary In For Loop

I am trying to make small project to improve myself. I couldnt understand one part while I was coding.

I am scraping the data from a website and I print data with a for loop. I get the UFC links for every athlete. System works very nice and there is no problem. I put id for every athlete when I print them by their name. I use a dictionary for their id number.

The problem is when I use dictionary as; id[x] = href['href'] the code works excellent

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it prints whatever I type in input(choose)

However when I try to use it as; id = {x : href['href'] the code doesnt work properly

just prints last value of the last key.

So my question is, why I cant get the specific href with the method of id = {x : href['href']

this is the working code you can try it

import requests
from bs4 import BeautifulSoup

input2 = str(input('type the name of the athlete: '))

payload = {'gender':'All', 'search':input2}
session = requests.Session()
p = session.get('https://www.ufc.com/athletes/all?', params=payload)

soup2 = BeautifulSoup(p.text, 'html.parser')

spesific_athlete = soup2.find_all('div','c-listing-athlete-flipcard__inner')
id = {}
for x, a in enumerate(spesific_athlete, 1):
    spesific_athlet = a.find('span','c-listing-athlete__name')
    
    href = a.find('a', 'e-button--black')

    #{key : value}
    id[x] = href['href']

    print(spesific_athlet.text, ': ', href['href'], '\n', x) 

choose = int(input('Which athlete do you choose(type a number): '))
print(id[choose])

>Solution :

When you use id[x] = href[‘href’], you’re adding a new key-value pair to the id dictionary for each athlete. So, by the end of the loop, you have a dictionary with all the athletes’ href values stored.

However, when you use id = {x: href[‘href’]}, you’re redefining the entire id dictionary in every iteration of the loop. So, by the end of the loop, you only have the last athlete’s href value stored in the dictionary.

To fix this, you can use id[x] = href[‘href’] to add a new key-value pair to the dictionary, just like you did before. Here’s the edited code:

import requests
from bs4 import BeautifulSoup

input2 = str(input('type the name of the athlete: '))

payload = {'gender':'All', 'search':input2}
session = requests.Session()
p = session.get('https://www.ufc.com/athletes/all?', params=payload)

soup2 = BeautifulSoup(p.text, 'html.parser')

spesific_athlete = soup2.find_all('div','c-listing-athlete-flipcard__inner')
id = {}
for x, a in enumerate(spesific_athlete, 1):
    spesific_athlet = a.find('span','c-listing-athlete__name')
    
    href = a.find('a', 'e-button--black')

    #{key : value}
    id[x] = href['href']

    print(spesific_athlet.text, ': ', href['href'], '\n', x) 

choose = int(input('Which athlete do you choose(type a number): '))
print(id[choose])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading