FAQ: List Comprehension - Capitals

This community-built FAQ covers the “Capitals” exercise from the lesson “List Comprehension”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Capitals

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This exercise asked to contain the string “capital, country” but this was not added in the solution. How are we expected to get it right if we are given faulty directions?

We will need some context for your question. Please post the URL of the lesson page.

Yeah it had me confused at first.
“Capital, country” refers to the format that is required for the answer to be correct.
In other words, your answer has to be ordered by capital, a comma, and the country, like this:
[‘Santiago, Chile’, ‘Paris, France’, ‘Copenhagen, Denmark’]

capitals = [“Santiago”, “Paris”, “Copenhagen”]
countries = [“Chile”, “France”, “Denmark”]

My answer : print(list(zip(capitals, countries)))

Given solution :
locations = [capital + ", " + country for (capital, country) in zip(capitals, countries)]

print(locations)

I can’t understand the difference. if anyone ? please tell me

Your output,

[('Santiago', 'Chile'), ('Paris', 'France'), ('Copenhagen', 'Denmark')]

The solution output,

['Santiago, Chile', 'Paris, France', 'Copenhagen, Denmark']
2 Likes

Greetings fellow users,

Is there a reason these code challenges are using the python 2.7 shell? This exercise, for example, would have been a lot easier using a python 3.6+ syntax:

locations = [f'{a}, {b}' for (a,b) in zip(capitals, countries)]

capitals = ["Santiago, ", "Paris, ", "Copenhagen, "]
countries = [“Chile”, “France”, “Denmark”]
locations = zip(capitals, countries)
locations = [item1 + item2 for (item1, item2) in locations]
print(locations)

My output:
[‘Santiago, Chile’, ‘Paris, France’, ‘Copenhagen, Denmark’]

My solution is incorrect. Why?

EDIT: locations = [item1 + ", " + item2
I added the ", " directly to the first list, so it wasn’t technically correct. The ", " in between fixed my problem.

i dont get why my code does not run. its like the same thing

capitals = [“Santiago”, “Paris”, “Copenhagen”]

countries = [“Chile”, “France”, “Denmark”]

locations = zip(capitals + countries)

locations = [item1 + ’ ’ + item2 for (item1,item2) in locations]

print(locations)

the solution says in last line

locations = [capital + ", " + country for (capital, country) in zip(capitals, countries)]

but zip isnt even being used before

zip() takes two or more arguments. Above you are creating a single list, meaning it is only one argument. Use comma instead of plus sign.

1 Like