Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to concatnates data to list

Using List comprhension
Using Append

First Method

Given data

url_list = [
    '/rankings/mens/player-rankings/3761',
    '/rankings/mens/player-rankings/1277',
    '/rankings/mens/player-rankings/170',
    # ... other URLs ...
]
Enter fullscreen mode Exit fullscreen mode

and you want to add "https://www.icc-cricket.com" to each element to make them absolute URLs, you can use a list comprehension to achieve this:

# Given list of relative URLs
url_list = [
    '/rankings/mens/player-rankings/3761',
    '/rankings/mens/player-rankings/1277',
    '/rankings/mens/player-rankings/170',
    # ... other URLs ...
]

# Add the base URL to each element
full_urls = ['https://www.icc-cricket.com' + url for url in url_list]

# Print the full URLs
for url in full_urls:
    print(url)

Enter fullscreen mode Exit fullscreen mode

This code will create a new list full_urls where each element is an absolute URL by adding "https://www.icc-cricket.com" to the beginning of each URL in the original url_list.

url_list = [
    '/rankings/mens/player-rankings/3761',
    '/rankings/mens/player-rankings/1277',
    '/rankings/mens/player-rankings/170',
    # ... other URLs ...
]

full_url_list = []  # Create a new list for full URLs

for url in url_list:
    full_url = "https://www.icc-cricket.com" + url
    full_url_list.append(full_url)

print(full_url_list)
Enter fullscreen mode Exit fullscreen mode

Second Method

url_list = []
tbody = soup.find('tbody')
if tbody:
    for tr in tbody.find_all('tr'):
        tds = tr.find_all('td')

        if len(tds) >= 0:
            second_td_content = tds[1].text
            a_tag = tds[1].find('a')
            url = a_tag['href']
            full_url = "https://www.icc-cricket.com/" + url
            url_list.append(full_url)         

url_list
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)