[Tutor] Question regarding lists and manipulating items in lists.

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Jan 16 01:00:39 CET 2013


On 15 January 2013 23:53, Scurvy Scott <etanes.rm at gmail.com> wrote:
>> > Anyways, the problem I'm having is I'm not really sure how to search a list
>> > for multiple elements and remove just those elements. Below is my code so
>> > far, and information y'all could provide would be appreciated. Thanks.
>>
[SNIP]
>
> What I meant to say is, I want to be able to search the generated list
> for a specific set of strings so like
>
> list = ['a','b','c','d','e','f','g']
> search = ['b','d','g']
> list.del[search]

How about:

new_list = [element for element in list if element not in search]

(It would be more efficient to use a set but it's not strictly necessary)


Oscar


More information about the Tutor mailing list