By Harshi


2019-05-15 16:31:31 8 Comments

I'm writing a loop to go through 2 lists and print sentences out of them but it's not working.

I've tried changing up the syntax but nothing is working.

states = ["virginia", "new jersey", "north carolina", "california"]
capitals = ["richmond", "trenton", "raleigh", "sacramento"]

for x, name in enumerate(states):
    for y name in enumerate(capitals):
        print("The capital of " + states[x] + "is " + capitals[y] + ".")

This is the error I keep getting.

File "<ipython-input-11-9f2d009ec38f>", line 2
    for y name in enumerate(capitals):
             ^
SyntaxError: invalid syntax

4 comments

@Daweo 2019-05-15 16:53:36

There is not need for nested loop here, as you have one-to-one relation in your data (every state has 1 capital and every capital has 1 state). You could use zip to create pairs or if you wish to use index value inside for loop you could do:

states = ["virginia", "new jersey", "north carolina", "california"]
capitals = ["richmond", "trenton", "raleigh", "sacramento"]
for i in range(len(states)):
    print("The capital of "+states[i]+" is "+capitals[i])

or using so called f-strings:

states = ["virginia", "new jersey", "north carolina", "california"]
capitals = ["richmond", "trenton", "raleigh", "sacramento"]
for i in range(len(states)):
    print(f"The capital of {states[i]} is {capitals[i]}")

In both case output is:

The capital of virginia is richmond
The capital of new jersey is trenton
The capital of north carolina is raleigh
The capital of california is sacramento

Note that my method will work correctly if number of elements of states is equal to number of elements of capitals.

@Relandom 2019-05-15 16:34:49

The invalid syntax is because you are missing ',' between y and name.

Anyway, you can use the build-in function zip.

for state, capital in zip(states, capitals):
    print("The capital of " + state + "is " + capital)

@Reedinationer 2019-05-15 16:36:40

Nice one, I always forget about zip(). You should change "is " to " is " though otherwise it will print weirdly (OP should do this too!)

@Relandom 2019-05-15 16:39:00

@Reedinationer hmmm... You are right! I skip this part. Usually, I prefer to use format or ',' instead of '+' (comma will add spaces by itself).

@Harshi 2019-05-15 16:50:59

Wow that was a really simple solution. I didn't even know the zip function existed. Thank you!!

@Reedinationer 2019-05-15 16:34:21

I don't think a nested for loop is necessary here. Your lists are the same length, so enumerate() is already telling you the index. I would simplify it to:

states = ["virginia", "new jersey", "north carolina", "california"]
capitals = ["richmond", "trenton", "raleigh", "sacramento"]

for x, name in enumerate(states):
    print("The capital of " + name + " is " + capitals[x] + ".")

Which yields:

The capital of virginia is richmond.

The capital of new jersey is trenton.

The capital of north carolina is raleigh.

The capital of california is sacramento.

@Praveenkumar 2019-05-15 16:33:36

You missed a "," in your second for loop for y, name in enumerate(capitals):

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

20 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 2113482 View
  • 3599 Score
  • 20 Answer
  • Tags:   python loops list

39 Answered Questions

[SOLVED] Loop through an array in JavaScript

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

62 Answered Questions

[SOLVED] Calling an external command from Python

11 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

20 Answered Questions

[SOLVED] What are metaclasses in Python?

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

35 Answered Questions

[SOLVED] How do I break out of nested loops in Java?

Sponsored Content