Home > Software design >  Extract elements from dynamic list as increases or decreases in python
Extract elements from dynamic list as increases or decreases in python

Time:02-09

Hello i have a dynamic list which increases or decreases accordingly from the extracted data from a site with BeautifulSoup

list = [a,b,c,d,.......]

how can i make the following fstring to be dynamic as the list increases or decreases?

fstr = f"show the following {list[0]}, {list[1]}, {list[2]}, {list[3]}, {list[etc......]}"

CodePudding user response:

myCommaSeparatedValues = ', '.join(myList)

This should work, then insert this list into your fstr variable:

fstr = f"Show the following {myCommaSeparatedValues}"

NOTE: never call a variable list since you are overwriting the built-in class list. Call it myList instead.

CodePudding user response:

you could use the join method:

mylist = [1,2,3,4,5]
fstr = f'show this: {', '.join(mylist)}'

or you could do this:

fstr = f'show this: {str(mylist).removeprefix("[").removesuffix("]")}'

output:

show this: 1, 2, 3, 4, 5
  •  Tags:  
  • Related