Does age need to be at the beginning or end of all_ages?

Pint for clarification:
It isn’t clear in #4 from the words “Create a new list called all_ages that combines age with the following list:” means
age + [numbers]
and not [numbers] + age.
This apparently makes a difference as to where age is inserted.

all_ages = age + [32, 41, 29] #age goes to beginning
#all_ages = [32, 41, 29] + age #age goes to the end
print(age)
print(all_ages)

This is important since if we print the zipped list items,

for x in name_and_age:
  print (x)

we want this…

('Ainsley', 42)
('Ben', 32)
('Chani', 41)
('Depak', 29)
2 Likes

But it says "the ages for Ainsley, Ben, and Chani:

[32, 41, 29]"

So it should be all_ages = [32, 41, 29] + age, right?

4 Likes

When we zip two lists of equal length, we get a zip object of the same length comprised of name, age pairs as tuples. This is not a list unless we cast it to one, and printing will consume the object. I’m not sure that we can extend a zip object (rather doubtful given the nature of iterators) so we have to go back to the original lists and extend them.

names = ['Ainsley', 'Ben', 'Chani']
ages = [32, 41, 29]

We can extend them with append() or by concatenating a list as follows,

names += ['Dupak']
ages += [29]

Or we can extend a list cast from a zip object. First, cast the list…

names_and_ages = list(names_and_ages)

That will consume the zip object but we have its tuples. Now we can append or concatenate another tuple, as suits…

names_and_ages += ('Dupak', 29)

which will give us,

[('Ainsley', 32), ('Ben', 41), ('Chani', 29), ('Dupak', 29)]

Bottom line, if we wish to continue using the data structure created with zip(), then it must be cast to a list, first.

2 Likes

apart from zipping i think we have a misunderstanding with the text. it says dupak’s age is 42, at first. in order to make the list right I made [xxxx] + age

3 Likes

Looks, to me, like you are correct. Depak’s age is 42, not 29.

1 Like

I’m having a similar problem. Is there a typo somewhere in the task? Because we should need to write all_ages = [[32, 41, 29] + age], right? That way, Depak’s age will be correctly associated with 42, since he is the 4th name on the list. When I wrote that as my answer, the software said my answer was wrong. But I wasn’t sure if it’s wrong because of the typo or because of my syntax. I did some trial and error using “print” to determine that I don’t need that extra set of brackets and that it’s enough to write all_ages = [32, 41, 29] + age.

2 Likes

Where is the bug? Everything looks fine, or at least in working order.

the instruction says " Depak’s age is 42 . Use .append() to add 42 to age ." That means “Age” comes after the list so it can be attached to “Depak” because of where its on the list. That’s what I did and it works for me.

[(‘Ainsley’, 32), (‘Ben’, 41), (‘Chani’, 29), (‘Depak’, 42)]

The instructions for this exercise are poorly written and the solution code gives the wrong answer.

Depak’s age is 42 . Use .append() to add 42 to age .

Solution code:

first_names = ['Ainsley', 'Ben', 'Chani', 'Depak']

age = []

age.append(42)

all_ages = age + [32, 41, 29]

name_and_age = zip(first_names, all_ages)

ids = range(4)

print(all_ages)

This results in Depak’s age being 29, not 42.

Is that your solution, or the given solution? As I understand it we have two lists, first_names and ages.

We wouldn’t need to create a new list, just append the existing one.

The above prepends to the list, not append, which would go to the right side, not the left.

Hi,

The solution pknytl has shown is the given one, which when printed as mentioned, results in Depak’s age being 29 not 42 as you get a syntax error trying to put your existing list of age (which holds Depak’s 42) after the new list to align with Depak’s position in the name list

Please demonstrate that error in the interactive console.

Not really sure what point is being made, here. Are you saying the member did it that way because that is how it was shown in the solution? It is not appending to the ages list.

[1, 2, 3, 4] += [5]

That will append a list to the list so that it becomes,

[1 ,2, 3, 4, 5]

which is the same effect we would get by appending the value…

[1, 2, 3, 4, 5].append(6)

giving,

[1, 2, 3, 4, 5, 6]

Hi,

I am probably not explaining it that well.

We have a list of names already in an order than end with Depak
We have created a list of ages that currently only hold the age of Depak = 42
We are asked to amend the list of age with the new supplied 32, 41, 29 (which are the ages for Ainsley, Ben and Chani respectively.
The issue we have is new additions will be added to the end of all_ages (as shown - 42, 32, 41, 29) thus when we associate ages with names Depak is now assigned to 29 - still being last on the list of names and now 29 coming at the end of all_ages

If you try to do:

all_ages = [32, 41, 29] + age

so that age comes at the end of the new variable (keeping Depak’s age at the end) you receive an error. It will only accept the existing var for age then the list in the creation of all_ages

Can we see that error message, please?

Gawd I swear I tried it so many times and received the error - now it passes through no worries! Maybe my session had locked either way don’t worry at least now I know it works! Sorry to have wasted your time

1 Like

Small detail

When we add an item to the end of the list, we append it.

When we concatenate a list with another list, we extend it. The operative term is with another list. Study the examples above to see that distinction.

Hi mtf, thanks for your reply.

I suggest you do the exercise in question whilst following the instructions to the letter. You will then understand what we are talking about rather than replying based on assumption.

Best,

P