FAQ: List Comprehension - Ages

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

Paths and Courses
This exercise can be found in the following Codecademy content:
Visualize data with Python

FAQs on the exercise Ages

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!

names = ["Jon", "Arya", "Ned"]
ages = [14, 9, 35]

names_ages =list(zip(names, ages))
print(names_ages)

users = ["Name: " + a + ", Age: " + str(b) for (a,b) in names_ages]

print(users)

From this exercise

Hi there,

I’m just wondering if this is the simplest / fastest approach to this question. I know it returns the correct result, but I’m not sure if it’s the acceptable/common way to get it.

Thanks!

If you don’t need to print names_ages then that variable is not needed, and we don’t need to populate a list object.

for a, b in zip(names, ages)

We can use string formatting to replace the concatentation and str casting.

f"Name: {a}, Age: {b}"
1 Like

Hello,
From this exercise

I’m getting a syntax error for this, looking for help on the issue.
Is this because f-strings are being evaluated first that is the issue, or did i make a mistake somewhere?

names = ["Jon", "Arya", "Ned"]
ages = [14, 9, 35]

users = [f"Name: {a}, Age: {b}" for (a, b) in zip(names,ages]

See anything missing?

Thanks for the reply, It’s still returning syntax error

users = [f"Name: {a}, Age: {b}" for (a, b) in zip(names,ages)]

Using your code lines,

>>> names = ["Jon", "Arya", "Ned"]
>>> ages = [14, 9, 35]
>>> users = [f"Name: {a}, Age: {b}" for (a, b) in zip(names,ages)]
>>> users
['Name: Jon, Age: 14', 'Name: Arya, Age: 9', 'Name: Ned, Age: 35']
>>> 

That’s my code:

names = [“Shilah”, “Arya”, “Kele”]
ages = [14, 9, 35]

users = [ "Name: " +n+ ", Age: " + str(a) for n,a in zip(names,ages) ]
print(users)

Using the available formatting methods we can write the above with a single string expression being appended to the list…

users = [f"Name: {n}, Age: {a}" for n, a in zip(names,ages)]

Read up on f-string formatting (which is used above). Note how we do not have to cast anything to str. The formatter does that for us.

2 Likes

Oh! that’s great and easier to write.
Thanks @mtf

1 Like

Just a heads-up: we can’t use f-strings for this exercise (or any of these code challenges, it seems) since the default shell is somehow set to python 2.7. This explains why the users above keep running into syntax errors.
As of writing (december 1st, 2020) the following:

import sys
print(sys.version)

returns
2.7.17 (default, Jul 20 2020, 15:37:01)
[GCC 7.5.0]

Deeply frustrating once one is used to python 3.6+

1 Like

At least we still have str.format() to fall back on.

'Name: {}, Age: {}'.format(a, b)

The hint of which version the shell is supported by is in exercise 5.

Make sure to divide by 2 , not 2.0 !

Python 2 integer division by 2 is effectively a right shift, equivalent to floor division.

right_shift = [x >> 1 for x in nums]
1 Like

Oh thank you. I totally missed that little nugget of information. I have not touched python 2 in ages and have effectively banished it from my mind, but I think it’d behove me to look back at it once in a while.

Greatly appreciated.

1 Like

F-strings don’t work with list comprehensions in codeacademy env, yet they work in my terminal (python 3) just fine. I’ve been told that f-strings are the standard in current python3 programing, and indeed I see them everywhere on stack overflow so… did I mess up somewhere or should I report this as a bug?

In the content of this exercise they write:

After you have cast an integer to a string, you can concatenate it to another string by using + :

“I have " + str(100) + " cats!”
“I have 100 cats!”
row_count = 20
"This spreadsheet has a row count of: " + row_count
“This spreadsheet has a row count of: 20”


My issue is - did they pass row_count to a string? I don’t see that. What is this meant to be an example of? When I ran their code, it didn’t work, because row_count was not a string. Am I being an idiot about something, or did they just make a content gaff?

Yeah, it looks like something slipped through there. You’re quite right, it’s a good example of the kind of concatenation you can’t do.

1 Like

Good catch, this has been fixed right up!

I tried like that
names = [“Shilah”, “Arya”, “Kele”]
ages = [14, 9, 35]

users = [f"Name: {name}, Age: {age}" for (name, age) in zip(names, ages)]

print(users)

and gave it
“SyntaxError: invalid syntax”

It’s mentioned a few times above that this lesson is based on Python2 so f-strings aren’t available. A good alternative has been provided-

1 Like