I’ve just started with python and I’m writing a script that returns the number of clicks for a bunch of links. I’m using a for loop to iterate through all the links and return the click count for each:
for url in short_urls:
query_params = {
'access_token': ACCESS_TOKEN,
'link': url.values()}
endpoint = ENDPOINT
response = requests.get(endpoint, params = query_params)
data = json.loads(response.content)
print "link_clicks:" + " " + str(data['data']['link_clicks'])
The results returned into terminal look like this:
link_clicks: 938
link_clicks: 63
link_clicks: 3921
link_clicks: 47
link_clicks: 21
What would be the best way to add all of those numbers and return a total? Ideally, I would like to be able to print the total as “total link_clicks: (total)” in terminal.
EDIT:
Thank you all for the answers.
I guess I was overcomplicating the solution in my head, for some reason I thought it would require creating a list to which each new integer from the for loop would have to be added and then individually adding each element of that new list via indexing to grab the total.
You use
sum()to add up elements in a list.Or simply add them in a variable..