Python – Odd Frequency Characters

Sometimes, while working with Python strings, we can have a problem in which we need to extract all the string characters which have odd number of occurrences. This problem can have applications in domains such as data domain and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_str = ‘geekforgeeks’
Output : [‘r’, ‘o’, ‘f’, ‘s’]

Input : test_str = ‘g’
Output : [‘g’]

Method #1 : Using defaultdict() + list comprehension + loop
The combination of above functions can be used to solve this problem. In this, we initialize the defaultdict() with integer and then perform frequency count. List comprehension is used to extract all the odd frequencies.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Odd Frequency Characters
# Using list comprehension + defaultdict()
from collections import defaultdict
  
# helper_function
def hlper_fnc(test_str):
    cntr = defaultdict(int)
    for ele in test_str:
        cntr[ele] += 1
    return [val for val, chr in cntr.items() if chr % 2 != 0]
  
# initializing string
test_str = 'geekforgeeks is best for geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Odd Frequency Characters
# Using list comprehension + defaultdict()
res = hlper_fnc(test_str)
  
# printing result 
print("The Odd Frequency Characters are : " + str(res))

chevron_right


Output :



The original string is : geekforgeeks is best for geeks
The Odd Frequency Characters are : ['k', 'i', 't', 'g', 'e', 'b']

 

Method #2 : Using list comprehension + Counter()
The combination of above functionalities can be used to solve this problem. In this, we use Counter() to count the frequency.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Odd Frequency Characters
# Using list comprehension + Counter()
from collections import Counter
  
# initializing string
test_str = 'geekforgeeks is best for geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Odd Frequency Characters
# Using list comprehension + Counter()
res =  [ chr for chr, count in Counter(test_str).items() if count & 1 ]
  
# printing result 
print("The Odd Frequency Characters are : " + str(res))

chevron_right


Output :

The original string is : geekforgeeks is best for geeks
The Odd Frequency Characters are : ['k', 'i', 't', 'g', 'e', 'b']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.