[Tutor] Is there a simpler way to remove from a set?

Leam Hall leamhall at gmail.com
Thu May 13 20:24:09 EDT 2021


I appreciate all the feedback and ideas on this, you helped me learn. Here's
what I've worked it down to, so far.

###
#!/usr/bin/env python3

full_list = ['one', 'two', 'three', 'hilly', 'sam', 'free bird',
   'gollum', 'one', 'one']

excludes = ['tw', 'f', 'g']

def sw(thing, sw_list):
   for sw_l in sw_list:
     if thing.startswith(sw_l):
       return True
   return False

trim_list = set(item for item in full_list if not sw(item, excludes))

print("Looking at trim_list")
for t in trim_list:
   print(t)

###


-- 
Site Reliability Engineer  (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)


More information about the Tutor mailing list