Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

charles mills
charles mills
1,856 Points

How can I make a remove Method that goes through my list?

I would like to make a remove method

something like

def type_check :

if the element in the list is type [String, boolean]

remove from list.

I took a look around at the documentation but I couldn't really get anywhere. I was attempting make a method to try to solve stage 2 of list redux.

If you can point me to some pseudo code and some docs maybe I can skratch up a question that the oracles at StackOverflow can look down on through their glasses, it they aren't fogged up by hot air.

Stephen Bone
Stephen Bone
12,359 Points

Hi Charles

Sorry just trying to understand further is this tied to a stage (as I can't seem to find it) or are you just trying to figure out some code?

Thanks Stephen

charles mills
charles mills
1,856 Points

yes sir it is tied to stage 2 of the list redux. The task is to find the boolean, the string and one other thing and delete it from the list . It sounds like they want a method right?

I feel that many of these questions are somewhat vague and lots of unexpected things can happen, like asking bakers to show up for a creampie casting couch....

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Here is one way to do it:

In [40]: def removeitems(list):
   ....:     remove_types = [str, bool]
   ....:     result = []
   ....:     for item in list:
   ....:         if not type(item) in remove_types:
   ....:             result.append(item)
   ....:     return result
   ....: 

In [41]: removeitems(['s', 1, True, 4, 's'])
Out[41]: [1, 4]

If you want edit the list in place, then assign result to list:

In [50]: def removeitems2(list):
    remove_types = [str, bool]
    result = []
    for count, item in enumerate(list):
        if not type(item) in remove_types:
            result.append(item)
    list[:] = result
   ....:     

In [51]: list = ['s', 1, True, 4, 's']

In [52]: removeitems2(list)

In [53]: list
Out[53]: [1, 4]
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's not a good idea to change a list while you're going through it. To solve the challenge, you don't need to write code like this, either.

Matthew Proudman
Matthew Proudman
11,879 Points

okay Kenneth sorry it on my phone late at night deleted the post