Combine similar characters in Python using Dictionary Get() Method - GeeksforGeeks

14 Apr, 2022
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.
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 :
Declare the list.Declare a dictionary.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.Separate all the keys and the values and store them in 2 different lists.Use the zip() function store the product of keys and their respective values in the result list.Display the result.
Declare the list.
Declare a dictionary.
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.
Separate all the keys and the values and store them in 2 different lists.
Use the zip() function store the product of keys and their respective values in the result list.
Display the result.
Example 1 : 
python3
# declaring the list of charactersmylist = ['g', 'e', 'e', 'k', 's', 'f',          'o', 'r', 'g', 'e', 'e', 'k', 's'] # declaring the dictionarydictionary = {} # counting the frequency of the keysfor key in mylist:    dictionary[key] = dictionary.get(key, 0) + 1 # storing the of keys and valuesk = list(dictionary.keys())v = list(dictionary.values()) # declaring the result listresult = [] # storing the product of keys and# their respective values in resultfor i, j in zip(k, v):    result.append(i * j)     # displaying the resultprint(result)
Output :
['gg', 'eeee', 'kk', 'ss', 'f', 'o', 'r']
Example 2 : 
python3
# declaring the list of charactersmylist = ['p', 'y', 't', 'h', 'o', 'n', 't',          'u', 't', 'o', 'r', 'i', 'a', 'l'] # declaring the dictionarydictionary = {} # counting the frequency of the keysfor key in mylist:    dictionary[key] = dictionary.get(key, 0) + 1 # storing the of keys and valuesk = list(dictionary.keys())v = list(dictionary.values()) # declaring the result listresult = [] # storing the product of keys and# their respective values in resultfor i, j in zip(k, v):    result.append(i * j)     # displaying the resultprint(result)
Output :
['a', 'h', 'i', 'l', 'n', 'oo', 'p', 'r', 'ttt', 'u', 'y']
clintra
sumitgumber28
python-dict
Python-dict-functions
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists