How do I print all values in a list with a for loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I print all values in a list with a for loop?

I am trying to make a module that prints all the values in a list (Mine are all Strings). This is my current code: def printList(List): for i in List: print(List[i] + " ") When I decide to use it, it gives a TypeError: list indices must be integers or slices, not str. Does anyone know what I did wrong or how to fix this? All help Appreciated.

23rd Dec 2017, 11:06 PM
ClassiccalCoder101
ClassiccalCoder101 - avatar
2 Answers
+ 2
If in your list all strings list[i] doesnt make sense in this case i should be number. You need to do it in this way for i in List: print(i+" ")
23rd Dec 2017, 11:17 PM
Dima Makieiev
Dima Makieiev - avatar
0
wait nevermind it actually works somehow! Knowing that i gives the value of List[i] is very handy!
23rd Dec 2017, 11:21 PM
ClassiccalCoder101
ClassiccalCoder101 - avatar