Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Manipulating Lists

what's the problem with this code ? Should in the_list all non-integer values to be removed.

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

index = 0

while True:
  if type(the_list[index]) != int:
    del the_list[index]
  index = index + 1
  if index >= len(the_list):
    break

2 Answers

Remember when you delete and index it shift everything over so what you should try is:

if type(the_list[index]) != int: del the_list[index] else: index = index + 1

So it only increment when an int is found. For example slot 0 will get deleted and when you do that slot 1 will become 0 and so forth. So when it delete False the list then moves to the False position and since you have it to increment the list gets skipped.

Right. Thank you so much for your response.

While your method will work with the afore mentioned tweaks I think you would be better served by a for loop here since it would eliminate the need for your index = index + 1.

the_list = ["a", 2, 3, 1, False, [1, 2, 3]] output_list = []

for value in the_list: if type(value) != int: output_list.append(value)

This will be a bit faster for a short list like this and will make your code a bit nicer to look at.

If you really want to keep your code style then one more simple change to make (in addition to what nighingale already told you) would be to replace index = index + 1 with index += 1. It does exactly the same thing but is a bit shorter, easier to look at, and quicker to type.

aha , tanx . Finally, I wrote this code:

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

index = 0
while index < len(the_list):
  if type(the_list[index]) != int:
    del the_list[index]
    index -=1
  index += 1

print(the_list)

And another question: Is there a for loop like C++ language in Python? like this :

forLoop.cpp
for(int counter = 0 ; counter < sampleValue ; counter++){
     //code...
}

That looks kind of like a list comprehension...

I will admit I haven't done any work with C++ since I was in highschool (many many many years ago) and I didn't really get programming at all then so I can't really say that this is what you are asking for.

If I was you I'd look up some tutorials on list comprehension as I think that is what you are looking for.

Actually I'm surprised that the treehouse python track doesn't include a section on list comprehensions. What's up with that Mr. Love?

Meme about list comprehension