Poking around looking for ways to clean up my Python code for MWC. It's probably time for me to let go of my 25-year-old BASIC programming experience, and stop making my Python look like BASIC:
Eliminating duplicates from a list using Sets. Currently I convert a list to a dictionary and back to do this:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> fruit = set(basket) # create a set from a list, removing duplicates >>> fruit set(['orange', 'pear', 'apple', 'banana']) >>> li =  # create a list from a set >>> for f in fruit: ... li.append(f) ... >>> li ['orange', 'pear', 'apple', 'banana'] >>> fruit = set() # create an empty set >>> fruit.add('fred') # adding to set >>> fruit.remove('fred') # removing from set >>> fruit.clear() # clear all data from a set >>> 'orange' in fruit # fast membership testing True >>> 'crabgrass' in fruit FalseSource: Dive into Python.
- Convert MWC keyword (tag) lists to sets - the duplicates annoy me.
- Using List Comprehensions as a building block for filters and mapping. What else are they good for?
- Filter data using Filters instead of for loops
- Add data using Map instead of other crazy structures