0

does list change numerate? why the results are different?

a = enumerate(range(10)) b = enumerate(range(1,10)) c=list(zip(a,b)) print('c is') print(c) a = enumerate(range(10)) print(list(a)) b = enumerate(range(1,10)) print(list(b)) d=list(zip(a,b)) print('d is') print(d)

24th Nov 2017, 9:45 PM
Learner Victor Ren
Learner Victor Ren - avatar
5 Antworten
+ 1
try in code playground a = enumerate(range(10)) print(1,list(a) ) print(2,a) print(3,list(a)) a is iterator, by using list (a) you exhausted the iterator. can also try a = enumerate(range(10)) print(1,next(a) ) print(2,list(a)) print(3,list(a))
25th Nov 2017, 10:23 AM
yuri
+ 4
#That I understood, you need to have variable names lists in your second examples if you want to print these lists too. #I have tried like this, it works very well. a = enumerate(range(10)) b = enumerate(range(1,10)) c=list(zip(a,b)) print('c is') print(c) a = enumerate(range(10)) x = list(a) print(x) b = enumerate(range(1,10)) y = list(b) print(y) d=list(zip(x,y)) print('d is') print(d)
24th Nov 2017, 11:16 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 4
#These websites are explaining well about enumerate. # http://book.pythontips.com/en/latest/enumerate.html # https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/enumerate # https://docs.python.org/3.6/library/functions.html#enumerate # I think because this is a built-in function / method. So it can't be readable or iterable without range,list or loop. # See last prints. # Here I found easier way than my previous post. You don't need to create variable name. You just add list front of enumerate. a = list(enumerate(range(10))) print(a) b = list(enumerate(range(1,10))) print(b) d=list(zip(a,b)) print('d is') print(d) print(enumerate(range(5))) print(list(enumerate(range(5))))
25th Nov 2017, 11:25 PM
Ferhat Sevim
Ferhat Sevim - avatar
0
c and d are different. c is a list, whereas d is empty list. Surprising! But why?
24th Nov 2017, 9:46 PM
Learner Victor Ren
Learner Victor Ren - avatar
0
Thank you, Ferhat Sevim. Your code works for me. But I still want a plain explanation on what occurs to the variable a after print(list(a)). Doest list(a) changes a? why d=list(zip(a,b)) is empty in my code? Thanks.
25th Nov 2017, 10:15 PM
Learner Victor Ren
Learner Victor Ren - avatar