+ 1

On numbered list

On python for kids, I saw a question like this. list=["yams","eggs","pepper"] create a loop that prints out the list(including the numbers) 1 yams 2 eggs 3 pepper Been racking my brains

4th Jul 2018, 1:39 PM
realcosmos
realcosmos - avatar
14 Answers
+ 3
this should work for i in range(len(list)): print(i+1,list[i])
4th Jul 2018, 1:59 PM
davy hermans
davy hermans - avatar
+ 3
try this, it should work as you looking for. list=["yams","eggs","pepper"] for c,v in enumerate(list,1): print(c,v)
4th Jul 2018, 4:54 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
realcosmos ,racking your brain is the fun part, no? Do you know how to print the list without the numbers?
4th Jul 2018, 1:48 PM
Qwertiony
Qwertiony - avatar
+ 1
Use enumerate insted of range and len! range and len looks ugly.
4th Jul 2018, 3:56 PM
Qwertiony
Qwertiony - avatar
+ 1
unpacking is like breaking iterable types (tuples, lists, dicts...)and assigning them to variables. example: a, b = [2, 3] ... a+b will be 5 each result of enumerate(list) is a paired tuple (number, list item). try to unpack your i...
4th Jul 2018, 4:38 PM
Qwertiony
Qwertiony - avatar
+ 1
nice Saga. so many options 3 different ways to go at this thing. thanks everyone
4th Jul 2018, 5:01 PM
realcosmos
realcosmos - avatar
+ 1
thanks qwertiony. hmm print (*i)😅😅😅😅😅
4th Jul 2018, 5:07 PM
realcosmos
realcosmos - avatar
0
yup for i in list: print(i)
4th Jul 2018, 3:40 PM
realcosmos
realcosmos - avatar
0
thanks Davy. I understand up to the print i+1, but the list [i] part is confusing. can you clarify? modified I got it it specifies the index of the item on the list to be printed.
4th Jul 2018, 3:41 PM
realcosmos
realcosmos - avatar
0
thanks qwertiony. that's a new one to me. for i in enumerate(list, 1): print (i) except the output comes with parentheses, commas, and quotation marks.☺
4th Jul 2018, 4:07 PM
realcosmos
realcosmos - avatar
0
do you know what is unpacking?
4th Jul 2018, 4:10 PM
Qwertiony
Qwertiony - avatar
0
no.
4th Jul 2018, 4:23 PM
realcosmos
realcosmos - avatar
0
saga just ruined next step :( SagaTheGreat 💯 , can you explain nested unpacking?
4th Jul 2018, 5:15 PM
Qwertiony
Qwertiony - avatar
4th Jul 2018, 5:24 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar