[Tutor] Query: lists

Alan Gauld alan.gauld at yahoo.co.uk
Tue Aug 14 19:16:57 EDT 2018


On 14/08/18 23:16, Peter Otten wrote:

> For a simple solution you do need a and b: leave words unchanged, append 
> words starting with "x" to a and words not starting with "x" to b.
> 
> Someone familiar with Python might do it with a sort key instead:

Or, for one definition of simple, a list comprehension?

filtered_list = [word for word in words if not word.startswith('x')]

Of course it doesn't retain the deleted words if that is important.
Or if you only want the deleted words simply remove the 'not'.

If you need all three results (original, filtered and removed)
then you need the original 'a' and 'b' lists as well as the
original 'words'.

It all depends on what exactly you need as an end result.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list