list of strings - remove commonalities of common strings
<p>I'm struggling to come up with a way to solve this problem (and how to come up with a name for this question on stackoverflow).</p>

<p>I need to somehow remove commonalities of strings preserving the remainder.  </p>

<p>Given a list such as the following:</p>

<pre><code>l = ('first',
     'first.second',
     'first.second.third',
     'a.b.c',
     'x.y.z',
     'x.y') 
</code></pre>

<p>I'm hoping to have a list output as:</p>

<pre><code>('first',
 'second',
 'third', 
 'a.b.c',
 'x.y',
 'z' ) 
</code></pre>

<p>as you can see, when <code>first.second</code> subtracts <code>first</code> and remains <code>second</code>. <code>first.second</code> is subtracted from <code>first.second.third</code> we get <code>third</code>.  <code>a.b.c</code> doesn't have anything to subtract from itself, so it remains.  <code>x.y</code> is subtracted from <code>x.y.z</code> and <code>z</code> remains.</p>

<p>I'm thinking maybe a <code>sort(key=len)</code> will be part of the solution, as well as some sort of recursion to end up with the string remainder.  I'm hoping for a clean way to remove commonalities of each string in the list. </p>

<p>If the output order is not important, this solution will give you the expected values.</p>

<p>This is by implementation almost the same as @brianpck's answer. But I used sorting to deal with the <code>"x.y.z"</code> problem. And some extra explanations.</p>

<pre><code>l = ('first',
     'first.second',
     'first.second.third',
     'a.b.c',
     'x.y.z',
     'x.y')

def clarify(lst):

    # Sort the list to deal with the order problem.
    # for ex. "x.y.z" deletes "x.y" if not sorted
    lst = sorted(lst)

    # Words encountered.
    words = set()

    new_list = []

    for elem in lst:

        # Divide elements using dots.
        divided = elem.split(".")

        # New element to be added to the result.
        new_elem = []

        # For each word in a divided element.
        for word in divided:
            # If a word in the element is not encountered before.
            # Add it to new element
            # Add it to words
            if word not in words:
                new_elem.append(word)
                words.add(word)
        # Join new element
        new_list.append(".".join(new_elem))

    return new_list

print clarify(l)

# Gives ['a.b.c', 'first', 'second', 'third', 'x.y', 'z']
# You can make this a tuple in the solution as in the input if you want.
</code></pre>

python algorithm