On Fri, 11 Dec 2009 12:49:42 -0800, Ed Keith wrote: > I have a problem and I am trying to find a solution to it that is both > efficient and elegant. > > I have a list call it 'l': > > l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr'] > > Notice that some of the items in the list start and end with an '*'. I > wish to construct a new list, call it 'n' which is all the members of l > that start and end with '*', with the '*'s removed. > > So in the case above n would be ['nbh', 'jkjsdfjasd'] > > the following works: > > r = re.compile('\*(.+)\*') [snip]
Others have suggested using a list comp. Just to be different, here's a version using filter and map. l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr'] l = map( lambda s: s[1:-1] if s.startswith('*') and s.endswith('*') else '', l) l = filter(None, l) -- Steven -- http://mail.python.org/mailman/listinfo/python-list