Return a list of strings from a function as opposed to a single string that contains the list
Question:
I am looking to produce a list which contains strings all in the form of URL’s I cannot figure out how to produce a list of strings.
I have tried creating a function that will concat some static values together along with a dynamic variable (ranging from 1-8), with the end goal producing URL’s in the form http://www.omdbapi.com/?apikey=blah=shameless&Season=1&Episode=2, http://www.omdbapi.com/?apikey=blah=shameless&Season=2&Episode=2,
and so on, with the Season= being the value I want to range from 1-8 incrementing by one.
The code I am using is
def numbers():
for n in range (1,9):
print (str(url+movie+'&Season='+str(n)+'&Episode=2'))
abc = numbers()
print(abc)
which produces
http://www.omdbapi.com/?apikey=blah=shameless&Season=[1, 2, 3, 4, 5, 6, 7, 8]&Episode=2
Again, I want a list of 8 strs, not a single str containing the 8 values I were hoping would represent the different element of each string.
Any help or a nudge in the right direction would be great!
Answers:
try list comprehension, below code should work for you.
def numbers():
return [str(url+movie+'&Season='+str(n)+'&Episode=2') for n in range (10) ]
abc = numbers()
print(abc)
I am looking to produce a list which contains strings all in the form of URL’s I cannot figure out how to produce a list of strings.
I have tried creating a function that will concat some static values together along with a dynamic variable (ranging from 1-8), with the end goal producing URL’s in the form http://www.omdbapi.com/?apikey=blah=shameless&Season=1&Episode=2, http://www.omdbapi.com/?apikey=blah=shameless&Season=2&Episode=2,
and so on, with the Season= being the value I want to range from 1-8 incrementing by one.
The code I am using is
def numbers():
for n in range (1,9):
print (str(url+movie+'&Season='+str(n)+'&Episode=2'))
abc = numbers()
print(abc)
which produces
http://www.omdbapi.com/?apikey=blah=shameless&Season=[1, 2, 3, 4, 5, 6, 7, 8]&Episode=2
Again, I want a list of 8 strs, not a single str containing the 8 values I were hoping would represent the different element of each string.
Any help or a nudge in the right direction would be great!
try list comprehension, below code should work for you.
def numbers():
return [str(url+movie+'&Season='+str(n)+'&Episode=2') for n in range (10) ]
abc = numbers()
print(abc)