On Apr 1, 3:57 am, Nico Grubert <nicogrub...@gmail.com> wrote: > Dear Python developers > > I have the following (sorted) list. > ['/notebook', > '/notebook/mac', > '/notebook/mac/macbook', > '/notebook/mac/macbookpro', > '/notebook/pc', > '/notebook/pc/lenovo', > '/notebook/pc/hp', > '/notebook/pc/sony', > '/desktop', > '/desktop/pc/dell', > '/desktop/mac/imac', > '/server/hp/proliant', > '/server/hp/proliant/385', > '/server/hp/proliant/585' > ] > > I want to remove all paths x from the list if there is a path y in the > list which is part of x so y.startswith(x) is true. > > The list I want to have is: > ['/notebook', '/desktop', '/server/hp/proliant'] > > Any idea how I can do this in Python? > > Thanks in advance > Nico
paths = ['/notebook', '/notebook/mac', '/notebook/mac/macbook', '/notebook/mac/macbookpro', '/notebook/pc', '/notebook/pc/lenovo', '/notebook/pc/hp', '/notebook/pc/sony', '/desktop', '/desktop/pc/dell', '/desktop/mac/imac', '/server/hp/proliant', '/server/hp/proliant/385', '/server/hp/proliant/585' ] seen = set() basepaths = [ seen.add(s) or s for s in paths if not any(s.startswith(ss) for ss in seen) ] gives: ['/notebook', '/desktop', '/server/hp/proliant'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list