Values are getting replaced in python list

Viewed 41

I am trying to get actual URLs of shortened URLs by using following code (I have replaced shortened URLs with others as stackoverflow doesn't allow them)

url_list = ['https://stackoverflow.com/questions/62242867/php-lumen-request-body-always-empty', 'https://twitter.com/i/web/status/1269102116364324865']

import requests    
actual_list =[]

for link in url_list:
  response = requests.get(link)
  actual_url = response.url
  actual_list = actual_url
print(actual_list)

At the end there is only last URL left in actual_url but I need each URL. Can someone tell me what am I doing wrong here?

3 Answers

You need to append the url's to the list.

actual_list =[]
for link in url_list:
  response = requests.get(link)
  actual_url = response.url
  actual_list.append(actual_url)

please try this, you need use append to add the item to list:

url_list = ['https://stackoverflow.com/questions/62242867/php-lumen-request-body-always-empty', 'https://twitter.com/i/web/status/1269102116364324865']

import requests    
actual_list =[]

for link in url_list:
  response = requests.get(link)
  actual_url = response.url
  actual_list.append(actual_url)
print(actual_list)

you need to append the url in list:

actual_list =[]
 for link in url_list:
     response = requests.get(link)
     actual_url = response.url
     actual_list.append(actual_url)

but you are assigning the url to actual_list variable

Related