# 5 Best Ways to Find Strings with Unique Characters in Python

Rate this post

π‘ Problem Formulation: Imagine you have a list of strings, and you aim to determine how many of these strings contain a single unique character. For instance, given the list `['a', 'aa', 'aaa', 'bcbc', 'ddd']`, the program should output `3` because ‘a’, ‘aa’, and ‘ddd’ contain only one unique character.

## Method 1: Using a For Loop and Set

This method involves iterating through each string in the list and using a set to eliminate duplicate characters. If the set’s size is one after insertion of all characters from a string, it means the string has only one unique character. The `len(set(string)) == 1` condition checks for this.

Here’s an example:

```def count_unique_character_strings(string_list):
count = 0
for string in string_list:
if len(set(string)) == 1:
count += 1
return count

string_list = ['a', 'aa', 'abc', 'cccc', 'de']
print(count_unique_character_strings(string_list))
```

Output:

`2`

We define a function `count_unique_character_strings()` that takes a list of strings as its argument. For each string in the list, the function creates a set of characters. If the set contains only one element, the function increments the count. In the provided example, ‘a’ and ‘cccc’ have one unique character, so the output is `2`.

## Method 2: Using List Comprehension

List comprehension in Python is an elegant way to create lists. This method employs the same logic as the first but condenses it into a single line within a list comprehension. It calculates the total count by summing up boolean values based on the unique character condition.

Here’s an example:

```string_list = ['bb', 'cccc', 'efg', 'hh', 'ijkl']
count = sum(len(set(s)) == 1 for s in string_list)
print(count)
```

Output:

`3`

The one-liner code calculates the number of strings with a unique character by summing the booleans resulting from evaluating `len(set(s)) == 1` for each string `s` in the string list. The output `3` corresponds to strings ‘bb’, ‘cccc’, and ‘hh’ meeting the condition.

## Method 3: Using a Filter and Lambda Function

Filtering in Python takes a function and an iterable, and “filters” the iterable by only returning items where the function returns True. Here we use a lambda function that checks our single unique character condition as the filter function.

Here’s an example:

```string_list = ['x', 'y', 'yz', 'zzzzz', 'abcd']
is_unique_char = lambda x: len(set(x)) == 1
unique_char_strings_count = len(list(filter(is_unique_char, string_list)))
print(unique_char_strings_count)
```

Output:

`3`

The `is_unique_char` lambda function serves as a condition for the `filter()` to find strings with only one unique character. Converting the result to a list and getting its length gives us the number of unique character strings in the list, which in this case is `3`.

## Method 4: Employing itertools.groupby

The `itertools.groupby()` function groups items in an iterable if an optional key function returns the same key. This method works by grouping identical adjacent characters and checking if there is just one group.

Here’s an example:

```from itertools import groupby

def count_unique_by_grouping(string_list):
return sum(1 for string in string_list if len(list(groupby(string))) == 1)

string_list = ['aaa', 'b', 'ccccc', 'dede', 'f']
print(count_unique_by_grouping(string_list))
```

Output:

`3`

The function `count_unique_by_grouping()` uses `groupby()` to group contiguous similar characters in each string. The strings that result in only one such group have a single unique character. It counts and sums up the occurrences of such strings, hence outputting `3`.

## Bonus One-Liner Method 5: Using map and count

This effective one-liner uses the built-in `map()` function to apply the unique character check to each element of the list, and the `count()` method to evaluate the condition.

Here’s an example:

```string_list = ['mnop', 'pppp', 'qr', 'ss', 'tuu']
count_unique = list(map(lambda s: s.count(s[0]) == len(s) and len(s) > 0, string_list)).count(True)
print(count_unique)
```

Output:

`2`

The lambda inside the `map()` function checks if all characters in a string are the same as the first character, indicating they are all identical, and also ensures the string is not empty. The resulting boolean list is then used by the `.count()` method to find True occurrences, resulting in the output `2`.

## Summary/Discussion

• Method 1: For Loop and Set. Simple and easy-to-understand approach. May not be the most Pythonic or efficient solution for large datasets.
• Method 2: List Comprehension. Pythonic and concise. Some may find it less readable than an explicit for loop.
• Method 3: Filter and Lambda Function. Makes use of functional programming techniques. Can be less intuitive for those unfamiliar with lambda and filter.
• Method 4: itertools.groupby. Efficient for sorted or partially sorted strings. Might be slightly more complex to understand than previous methods.
• Bonus Method 5: One-Liner using map and count. Extremely compact code. Requires understanding of map and count functions and lambda expressions.