Passing the same arguments to append() gives me different outputs (single vs double bracket list) and I don't understand why

Hi everyone,

I just found this interesting and I don’t know what it means. I was doing the Python Lists: Medical Insurance Estimation Project on the data scientist path, and one of the instruction states the following:

"We want to add our estimated insurance data for Maria, Rohan, and Valentina to the estimated_insurance_data list.

Use .append() to add (“Maria”, maria_insurance_cost) to estimated_insurance_data. Do the same for Rohan and Valentina." (This refers to a previous list created using zip() where names and insurance costs are zipped from previous lists and calculations)

So I thought: I either add each item one by one, or dump them all at once by passing a slice of the size of the whole list to append(). I got curious if these two approaches would produce the same output. So first I tried:

estimated_insurance_data.append(insurance_data[0])
estimated_insurance_data.append(insurance_data[1])
estimated_insurance_data.append(insurance_data[2])
print(estimated_insurance_data)

Which outputs

[('Maria', 4150.0), ('Rohan', 5320.0), ('Valentina', 35210.0)]

And then I tried

estimated_insurance_data.append(insurance_data[0:2])

Which outputs

[[('Maria', 4150.0), ('Rohan', 5320.0), ('Valentina', 35210.0)]]

Notice the double brackets instead of single ones compared to passing arguments one by one. Passing the whole list using

estimated_insurance_data.append(insurance_data)

Gave the same double bracket output as passing a slice. And I don’t understand the difference, whether one is wrong and the other one is right, and what is the meaning and implications of the double brackets in the output.

Could anyone explain this to me?

Thanks!

Hi,
append adds only one item onto the end of a list.
So, for your first example it’s adding the name and insurance cost together as one entry.
In the others it’s adding the whole list as a single entry (so you end up with a list with another list in it - hence the double brackets).
So;
estimated_insurance_data[0]
would just be
(‘Maria’, 4150.0)
for the first example, but;
[(‘Maria’, 4150.0), (‘Rohan’, 5320.0), (‘Valentina’, 35210.0)]
for the second.

If you were to do the second method again, you’d end up with a list with two lists in it.
i.e.;
[ [ list1 ] , [ list2 ] ]

As with most things, how right or wrong it is depends on what you intend to use it for.
The second, in this case, would probably be more fiddly to work with and pluck out the bits of data you wanted.

1 Like

Yes, that makes sense and the possibility of a “list within a list” came to me like 5 mins after posting the OP, but I’m glad someone else confirmed it.

It seems a bit weird that the function just adds one item to the end of the list and it’s not more plastic (i.e. recursive) in that regard, but maybe there are other functions that I still haven’t learnt in the career path that do the job of .append recursively? Or am I condemned to write a loop that invokes .append for every i in a list if I wanted to append them all to another list without passing the latter as a list?

.extend should save your soul from endless looping! It takes in an iterable and adds each to the end of the list in turn - so if you can loop through it, then you can use it with extend.

1 Like

Thanks, that’s very hepful =)

1 Like