9

I'm looking for a way to remove duplicate entries from a Python list but with a twist; The final list has to be case sensitive with a preference of uppercase words.

For example, between cup and Cup I only need to keep Cup and not cup. Unlike other common solutions which suggest using lower() first, I'd prefer to maintain the string's case here and in particular I'd prefer keeping the one with the uppercase letter over the one which is lowercase..

Again, I am trying to turn this list: [Hello, hello, world, world, poland, Poland]

into this:

[Hello, world, Poland]

How should I do that?

Thanks in advance.

4
  • which version of python are you using? Jul 27, 2014 at 16:51
  • How should I do that? - iterate over the list or look in the list to determine if there are duplicates then see if any of the duplicates are title case str.istitle() , keep the titlecase duplicates.
    – wwii
    Jul 27, 2014 at 16:57
  • @wwi How should I do that was just used as a polite way of asking suggestions on how I should approach my issue. For instance, I had never come across istitle() so thanks for the heads-up.
    – stratis
    Jul 27, 2014 at 17:21
  • @inspectorG4dget Python 2.7.6 64bit on Windows 7
    – stratis
    Jul 27, 2014 at 17:22

4 Answers 4

10

This does not preserve the order of words, but it does produce a list of "unique" words with a preference for capitalized ones.

In [34]: words = ['Hello', 'hello', 'world', 'world', 'poland', 'Poland', ]

In [35]: wordset = set(words)

In [36]: [item for item in wordset if item.istitle() or item.title() not in wordset]
Out[36]: ['world', 'Poland', 'Hello']

If you wish to preserve the order as they appear in words, then you could use a collections.OrderedDict:

In [43]: wordset = collections.OrderedDict()

In [44]: wordset = collections.OrderedDict.fromkeys(words)

In [46]: [item for item in wordset if item.istitle() or item.title() not in wordset]
Out[46]: ['Hello', 'world', 'Poland']
1
  • A compact and very elegant solution. +1 for maintaining the original order!
    – stratis
    Jul 27, 2014 at 17:18
6

Using set to track seen words:

def uniq(words):
    seen = set()
    for word in words:
        l = word.lower()  # Use `word.casefold()` if possible. (3.3+)
        if l in seen:
            continue
        seen.add(l)
        yield word

Usage:

>>> list(uniq(['Hello', 'hello', 'world', 'world', 'Poland', 'poland']))
['Hello', 'world', 'Poland']

UPDATE

Previous version does not take care of preference of uppercase over lowercase. In the updated version I used the min as @TheSoundDefense did.

import collections

def uniq(words):
    seen = collections.OrderedDict()  # Use {} if the order is not important.
    for word in words:
        l = word.lower()  # Use `word.casefold()` if possible (3.3+)
        seen[l] = min(word, seen.get(l, word))
    return seen.values()
2
  • How will this have a preference for capitalized words? Jul 27, 2014 at 16:56
  • @TheSoundDefense, I updated the answer. Thank you for pointing it. +1 your answer for original idea of using min.
    – falsetru
    Jul 27, 2014 at 17:04
3

Since an uppercase letter is "smaller" than a lowercase letter in a comparison, I think you can do this:

orig_list = ["Hello", "hello", "world", "world", "Poland", "poland"]
unique_list = []
for word in orig_list:
  for i in range(len(unique_list)):
    if unique_list[i].lower() == word.lower():
      unique_list[i] = min(word, unique_list[i])
      break
  else:
    unique_list.append(word)

The min will have a preference for words with uppercase letters earlier on.

0

Some better answers here, but hopefully something simple, different and useful. This code satisfies the conditions of your test, sequential pairs of matching words, but would fail on anything more complicated; such as non-sequential pairs, non-pairs or non-strings. Anything more complicated and I'd take a different approach.

p1 = ['Hello', 'hello', 'world', 'world', 'Poland', 'poland']
p2 = ['hello', 'Hello', 'world', 'world', 'Poland', 'Poland']

def pref_upper(p):
    q = []
    a = 0
    b = 1

    for x in range(len(p) /2):
            if p[a][0].isupper() and p[b][0].isupper():
                    q.append(p[a])
            if p[a][0].isupper() and p[b][0].islower():
                    q.append(p[a])
            if p[a][0].islower() and p[b][0].isupper():
                    q.append(p[b])
            if p[a][0].islower() and p[b][0].islower():
                    q.append(p[b])
            a +=2
            b +=2
    return q

print pref_upper(p1)
print pref_upper(p2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.