[Tutor] Sets and Python; new shiny tool syndrome?

Liam Clarke ml.cyresse at gmail.com
Wed Dec 28 03:55:40 CET 2005


Hi all,

I just tried out sets for the first time, and I'm finding multiple
uses for them, particularly for replacing  and simplifying what would
normally be one or two list comprehensions i.e.

	def parseKws(self, kw_data):
		ignoreSet = set(['import', 'from', 'as', ' ', ''])
		kws = set([])
		for line in kw_data:
			line = line.rstrip("\n")
			if "," in line: 	line = line.replace(",", " ")
			if ";" in line: 	line = line.replace(";", " ")
			l = set(line.split(" "))
			k = l.difference(ignoreSet)
			kws.update(k)

instead of

l = line.split(" ")
k = [ item for item in l if not (item in ignoreSet or item in kws) ]
kws.extend(k)

(Above just gets module names from various import statements.)

However, I'm reminded of the joke about you can tell what chapter
someone reading the Patterns book by the Gang of Four is up to by what
new pattern they're trying to use the next day, no matter the problem.

Are there any drawbacks to sets that I need to watch out for?

Regards,

Liam Clarke


More information about the Tutor mailing list