Why myList = [range(1,10)] does not return a list in python? -
this question has answer here:
- python range( ) not giving me list 1 answer
i tried this:
mylist = [range(1,10)] print(mylist)
and got output:
range(1, 10)
why did not returned list [1,2,3,4,5,6,7,8,9]?
you running example in python3, range
function returns iterable. therefore, have pass generator list
function force expression give full list:
l = list(range(10))
output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
with generator, can iterate on so:
in function_that_yields_generator(): #do
you can use function next()
elements generator. since range function iterable not iterator, can use this:
l = range(10) new_l = iter(l) >>next(new_l) 0 >>next(new_l) 1 >>next(new_l) 2
etc.
for iterator, can this:
>>s = function_that_yields_generator() >>next(s) #someval1 >>next(s) #someval2
Comments
Post a Comment