FAQ: List Comprehension - First Character

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

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

FAQs on the exercise First Character

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!

Why talk about using syntax [index], when its not even in the answer. its just confusing

Would you please provide a link to the exercise that you are referring to?

Question for lesson:
https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-2-introduction-to-python/modules/ida-2-4-list-comprehension/lessons/lists/exercises/first-character

I came here for help but there isn’t any for this lesson so I will post my answer and hope that someone can show me how to resolve this more efficiently: (I don’t know how to do ‘for/next’ loops yet)

names = [“Elaine”, “George”, “Jerry”, “Cosmo”]

Extract each name into it’s own ‘list’

name_0 = [names[0]]
name_1 = [names[1]]
name_2 = [names[2]]
name_3 = [names[3]]

Extract the first letter for each name and add it to

the list ‘first_character’

characters = [var1 for var1 in name_0[0]]
first_character = [characters[0]]

characters = [var1 for var1 in name_1[0]]
first_character += [characters[0]]

characters = [var1 for var1 in name_2[0]]
first_character += [characters[0]]

characters = [var1 for var1 in name_3[0]]
first_character += [characters[0]]

print(first_character)

One might take it to mean wrap it in brackets, like a list, but that is not what is actually meant, if I’m reading this problem correctly.

names = ['Elaine', 'George', 'Jerry', 'Cosmo']
name_0 = [names[0]]
print (name_0)    # ['Elaine']

All we’ve done above is add more complexity to working with the variable.

Instead, one will need to employ the built in list constructor…

name_0 = list(names[0])
print (name_0)    # ['E', 'l', 'a', 'i', 'n', 'e']

Still reviewing; expect future edits.

1 Like

I think I found what I was looking for in subsequent lessons, a simpler solution to the First Character problem is:

names = [“Elaine”, “George”, “Jerry”, “Cosmo”]

first_character = [names[0][0], names[1][0], names[2][0], names[3][0]]
print(first_character)

The answer to the problem is:

[‘E’, ‘G’, ‘J’, ‘C’]

I would explain this better but I’m new to Python and programming and I don’t know what any of the various elements are called. I’m also new to this forum and don’t know the proper posting standards yet.

2 Likes

Hi,

Just solved this as well.

a simpler way is to do the following:

names = [“Elaine”, “George”, “Jerry”, “Cosmo”]

first_character = [x[0] for x in names]

print(first_character)

for me matt from ohio has this all right down to a tea

[operation for item in list ] remember square brackets for example

[first_character = [name[0] for name in names]

so we replace the word “operation” with the name[0] to get all the characters in the 0 in the memory, and the word item is replaced by the whatever is stored in the variable
and names is the actual variable it is stored in i think this makes sense if not then let me know and i will try and explain it better thanks Ian

so just remember square brackets and this little sentence

[operation for item in list ]

replace the word operation with
whatever action you want to perform and the word item with number, name , string etc whatever is stored in your variable and then replace the word list with the name of the variable
in this instance it is

[operation for item in list]
instead of having above you need this
operation item list
[first_character = [name[0] for name in names]

i think this is making sense if not then let me know thanks Ian