Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to add single element in list at different position using python webscraping

At End---->using append
At Starts--->my_list.insert(0, value_to_add)
At Specific Index----->my_list.insert(index_to_insert, value_to_add)
Enter fullscreen mode Exit fullscreen mode

At End

my_list = [1, 2, 3, 4]
value_to_add = 5

my_list.append(value_to_add)

print(my_list)  # Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

=====or==========

all_match.append(rank_match.text)

Enter fullscreen mode Exit fullscreen mode

At Starts

my_list = [2, 3, 4]
value_to_add = 1

my_list.insert(0, value_to_add)

print(my_list)  # Output: [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

========or=======

all_match.insert(0, rank_match.text)
all_match
Enter fullscreen mode Exit fullscreen mode

At Specific Index

my_list = [1, 2, 3, 4]
value_to_add = 5
index_to_insert = 2  # Insert at index 2 (0-based index)

my_list.insert(index_to_insert, value_to_add)

print(my_list)  # Output: [1, 2, 5, 3, 4]
Enter fullscreen mode Exit fullscreen mode

In Webscrapping

My data
Image description

Image description

Coding

match_point = soup.find_all('td', class_="table-body__cell u-center-text")
match_point
Enter fullscreen mode Exit fullscreen mode

output
Image description

all_match_point=[]
for tag in match_point:    
    all_match_point.append(tag.text.strip())
all_match_point
Enter fullscreen mode Exit fullscreen mode

Image description

match_data = match_point[::2]
match_data
Enter fullscreen mode Exit fullscreen mode

Image description

rank_match = soup.find('td', class_="rankings-block__banner--matches")
rank_match.text
Enter fullscreen mode Exit fullscreen mode

output

'27'
Enter fullscreen mode Exit fullscreen mode
all_match=[]
for tag in match_data:    
    all_match.append(tag.text.strip())
all_match
Enter fullscreen mode Exit fullscreen mode

output

Image description

Append at end of list

all_match.append(rank_match.text)
Enter fullscreen mode Exit fullscreen mode

output
['27',
'40',
'28',
'23',
'31',
'33',
'37',
'21',
'38',
'30',
'33',
'24',
'28',
'42',
'28',
'31',
'24',
'41',
'27']

Append at start of list

all_match.insert(0, rank_match.text)
all_match
Enter fullscreen mode Exit fullscreen mode

output

Image description

Top comments (0)