Home > Mobile >  Python - How to append variables in a dict
Python - How to append variables in a dict

Time:04-29

I'm kinda new at this. I am using a for loop to get an ID from a .csv file and storing that data into a variable. Inside the for loop, there is a conditional to do some "web scraping" (I just need to know a value from the website for that ID). Then a conditional where I say something like

for u in df['ID']: 

    url = f"example.com/{u}" 
    response = requests.get(url)
    html = BeautifulSoup(response.text, 'html.parser')
    finder = html.find("something about BeautifulSoup library")
    my_variable = finder.get_text()

    if "hello world" in my_variable:
        final_variable = "hello world"
    elif "hi world" in my_variable:
        final_variable = "hi world"
    else:
        pass

and this part of the code works great. The bad things start when I try to store that data into a dict.

for what I did something like:

dictionary = {}
dictionary[f'{u}'] = risk

but then realized how dumb I am and that the dict was just rewritting itself.

I also tried to do an append but I am not being able to use it for both key and value.

The key should be the ID in the .csv (or "u" in the code) and the value must be final_variable.

I think the right thing to do is add an append but I don't know how to do that.

Thanks!

CodePudding user response:

To expand a bit on my comment - using dictionary[key] = value is the correct way to add to a dictionary, but if you want to do this once for every loop iteration then you must initialise it outside of the loop. See the following for a complete (if contrived) example.

# Initialise an empty dictionary
my_dict = {}

for i, letter in enumerate("abcde"):
    # Maybe some HTML parsing...

    # Add a new mapping to the dictionary each loop iteration
    my_dict[letter] = i

print(my_dict)  # {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

Also note that the f'{u}' is probably unnecessary - depending on your needs you could probably just use u itself as the dictionary key. If you really do need to convert it to a string, the idiomatic way for a single value would be str(u).

  • Related