-1

I have a method called getsomething(url) which hits a url and gets some details. This method returns a dictionary. Now as the url changes I want this dictionary which is already created to get updated with new items. How can this be done?

I have tried this:

url_list = ['http:something.com','anotherthing.com']

def getdetails(url):
    dict = {}
    # some lines of code here...
    return dict

it is getting called here:

for url in url_list: 
    result = {}
    result = result.update(getdetails(url))

I am getting error like nonetype object is not iterable

I need the result of both the url in same dict

Guy
  • 46,488
  • 10
  • 44
  • 88
  • Possible duplicate - https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression ? Since you are trying to merge dictionaries – verisimilitude Nov 06 '19 at 10:28
  • 3
    Assigning `{}` to python keyword `dict` is very bad way to write code. – Maciej M Nov 06 '19 at 10:29
  • `getdetails(url)` is returning `None` so this becomes `result = result.update(None)` hence the error. Make sure your list of urls is valid – Yugandhar Chaudhari Nov 06 '19 at 10:31
  • this list of urls i have given here is obviously invalid, as its just an example. I dont understand what do you mean by "getdetails(url) is returning None " – shreya srinivas Nov 07 '19 at 05:38
  • `dict.update()` returns None. You can just do `result.update(...)` – Cireo Nov 09 '19 at 03:16

1 Answers1

0

to update a dictionary u need to know how a dictionary look.

#// empty dictionary
dic = {}

#// some itmes on dictionary
dic2 = {"one":1, "two":3}
print(dic2)

#// so to update one itme first u need to specifi what item (index) need to be updated
dic2.update(two=2)

print("new =", dic2)

Terminal:

{'one': 1, 'two': 3}
new = {'one': 1, 'two': 2}

Also check this. Is not a shame to search on google believe me. I make this all time is normal. https://www.journaldev.com/23232/python-add-to-dictionary

ALso make a check state to if that "item" == None or not and if is !=None them make the updates "else" try to check the error and to see what is happen there.