Rob B.
Rob B.

Reputation: 128

How can I print each element of this Python list?

I'm trying to make this code print out the names. However, CodeAcademy says that it is wrong perhaps it because I have "" around the names around them when they are printed. What I wonder is how do I print out each name line by line. I'm thinking I need to use len.

 names = ["Adam","Alex","Mariah","Martine","Columbus"]
 for x in [names]:
     print x

Upvotes: 0

Views: 233

Answers (3)

just remove [ and ] around names variable i.e

for x in names:

that would print what you have asked for.

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599580

You're creating an extra list. You should just be doing:

for x in names:

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121714

You are almost there; don't make names a nested list:

for x in names:
    print x

You are nesting the names list; you put it inside another list, which then has just one element. Looping over that will run once, and print the whole list object.

In other words, names is already a list, and the for statement will loop over it directly without any extra list syntax.

Upvotes: 4

Related Questions