Combine similar characters in Python using Dictionary Get() Method

Let us see how to combine similar characters in a list.

Example :

Input : [‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’]
Output : [‘gg’, ‘eeee’, ‘kk’, ‘ss’, ‘f’, ‘o’, ‘r’]

We will be using the get() method of the dictionary class.

dictionary.get()

The get() method returns the value of the item with the specified key.



Syntax : dictionary.get(key name, value)
Parameters :

  • keyname : The key name of the dictionary item.
  • value : (Optional) If the specified key does not exist, then a value is returned.

Returns : Value of the item with the specified key

Algorithm :

  1. Declare the list.
  2. Declare a dictionary.
  3. Iterate over the list, using the get() method, if a new key is found, then the value 0 is assigned to it and 1 is added making the final value 1. Else if the key is repeated, then 1 is added to the previously calculated value. So this way, now each key has a value assigned to it, and frequency of all characters is recorded.
  4. Separate all the keys and the values and store them in 2 different lists.
  5. Use the zip() function store the product of keys and their respective values in the result list.
  6. Display the result.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# declaring the list of characters
mylist = ['g', 'e', 'e', 'k', 's', 'f'
          'o', 'r', 'g', 'e', 'e', 'k', 's'
  
# declaring the dictionary
dictionary = {} 
  
# counting the frequency of the keys
for key in mylist: 
    dictionary[key] = dictionary.get(key, 0) + 1
  
# storing the of keys and values
k = list(dictionary.keys())
v = list(dictionary.values())
  
# declaring the reslt list
result = []
  
# storing the product of keys and 
# their respective values in result
for i, j in zip(k, v):
    result.append(i * j)
      
# displaying the result
print(result)

chevron_right


Output :

['gg', 'eeee', 'kk', 'ss', 'f', 'o', 'r']

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# declaring the list of characters
mylist = ['p', 'y', 't', 'h', 'o', 'n', 't'
          'u', 't', 'o', 'r', 'i', 'a', 'l']
  
# declaring the dictionary
dictionary = {} 
  
# counting the frequency of the keys
for key in mylist: 
    dictionary[key] = dictionary.get(key, 0) + 1
  
# storing the of keys and values
k = list(dictionary.keys())
v = list(dictionary.values())
  
# declaring the reslt list
result = []
  
# storing the product of keys and 
# their respective values in result
for i, j in zip(k, v):
    result.append(i * j)
      
# displaying the result
print(result)

chevron_right


Output :

['a', 'h', 'i', 'l', 'n', 'oo', 'p', 'r', 'ttt', 'u', 'y']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.