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

5 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.

@Mahmoud Elshahat 2019-05-15 16:38:01

You should try this:

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

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

output:

The capital of virginiais richmond.
The capital of new jerseyis trenton.
The capital of north carolinais raleigh.
The capital of californiais sacramento.

@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

21 Answered Questions

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

  • 2009-02-06 22:47:54
  • Joan Venge
  • 1765017 View
  • 3066 Score
  • 21 Answer
  • Tags:   python loops list

26 Answered Questions

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

21 Answered Questions

10 Answered Questions

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

39 Answered Questions

[SOLVED] Loop through an array in JavaScript

14 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

38 Answered Questions

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

15 Answered Questions

[SOLVED] What are metaclasses in Python?

56 Answered Questions

[SOLVED] Calling an external command in Python

33 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 2704286 View
  • 2353 Score
  • 33 Answer
  • Tags:   python datetime time

Sponsored Content