enumerations | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

enumerations

numbers=format([i for i in range(10)]) numbs=str([1,2,3,4,5,6,7,8,9]) test=['1','2','3','4','5'] for i in enumerate(numbs): print(i) when i enumerate either of the formated strings i am getting a list including brackets, spaces and commas. Why is this happening?

8th Nov 2020, 2:52 PM
Sean
Sean - avatar
1 Answer
+ 4
Because str function directly convert the list as string like numbs = str ([1,2,3,4,5,6,7,8,9]) >>numbs >>"[1,2,3,4,5,6,7,8,9]" You are iterating direct string. So taking the each character of the string in the each iteration the final for loop is similar to for i in enumerate("[1,2,3,4,5,6,7,8,9]"): print(i) first it print (0,"[") if you want to convert the each element of the array into string then you should do like this numbs = [str(i) for i in range(1,10)] so you will get a character array then use enumerate function 👇 numbs = [str(i) for i in range(1,10)] for i in enumerate(numbs): print (i)
8th Nov 2020, 2:57 PM
Kelvin Paul
Kelvin Paul - avatar