Python Python Collections (2016, retired 2019) Lists Removing items from a list

Dylan Chuckry
Dylan Chuckry
2,497 Points

What am I doing wrong?

I might be drunk, help please :(

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.insert(0, messy_list.pop(3))
fixed_list = []

for each in messy_list:
    if each == int:
        fixed_list.append(each)
    else:
        continue

messy_list = fixed_list

3 Answers

John Lack-Wilson
John Lack-Wilson
8,156 Points

Hey Dylan, thanks for your question.

There are loads of ways to do this, although I quite like your approach because it is very explicit in what you are trying to do. The problem is related to your conditional for checking if each == int. You want to instead check the type of element in the messy_list array. See here for details about the type() builtin.

Here's a simple example:

an_integer = 1

if type(an_integer) is int:
    print("An Integer!")
Dylan Chuckry
Dylan Chuckry
2,497 Points

Thank you for the tip John! 'type()' seems quite useful. Would you mind giving me a quick explanation as to why using the '==' comparison doesn't word the same way?

I have ran into a new problem now though.

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.insert(0, messy_list.pop(3))
messy_list_copy = messy_list

for each in messy_list_copy:
    if type(each) is not int:
        messy_list.remove(each)

It returns the integers as well as the list. I assume this is because the data types inside of the list are all ints as well. I know I can solve it by creating another if statement and removing list data types but I am wondering of there is a more proper way of solving it?

John Lack-Wilson
John Lack-Wilson
8,156 Points

Great question! I may not be 100% correct with what I am saying here, but as far as I'm aware, the reason why if each == int doesn't work is because it is making the comparison between a reference i.e. the each part and a type i.e. the int part. The actual value that the reference is referring to may be an int, however the reference is not. That's the key thing to remember here, is that variables are simply referencing something, they are not the actual value or type. I hope that is both correct and makes sense!

The new issue seems to be related to checking if it is not an int. If you go back to your previous approach where you are appending to a new empty list if it is an int, then that should pass the challenge for you.

Dylan Chuckry
Dylan Chuckry
2,497 Points

Ah, that makes sense thank you. And yes I was doing that but the challenge requires you to use del or .remove.

John Lack-Wilson
John Lack-Wilson
8,156 Points

Ahh yes you're right! I'd forgotten the challenge does specify that (although it does pass without using either of those functions).

If you'd like to pass the challenge by using .remove() or del, then you should use list.copy(messy_list) rather than assigning messy_list_copy to messy_list. The difference is when you say messy_list_copy = messy_list, you are assigning them to the exact same list. This has issues when you are iterating over the list because any changes you make are reflected on both messy_list and messy_list_copy. Whereas using messy_list_copy = list.copy(messy_list), you are making a copy of the list and then assigning it to that. Any changes made to one of the lists will not change the other.