Given a list of elements, assign similar alphabet to same element.
Input : test_list = [4, 5, 2, 4, 2, 6]
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’, ‘d’]
Explanation : Alphabets assigned to elements as occurring.Input : test_list = [4, 5, 2, 4, 2, 6]
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’]
Explanation : Alphabets assigned to elements as occurring.
Method #1 : Using ascii_lowecase() + loop + list comprehension
In this, we extract all lowercase alphabets using lowercase(), and create dictionary mapping same element to similar character, post that we flatten that to appropriate index using list comprehension.
Python3
# Python3 code to demonstrate working of # Assign Alphabet to each element # Using ascii_lowecase() + loop + list comprehension import string # initializing list test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5] # printing list print("The original list : " + str(test_list)) temp = {} cntr = 0for ele in test_list: if ele in temp: continue # assiging same Alphabet to same element temp[ele] = string.ascii_lowercase[cntr] cntr += 1 # flattening res = [temp.get(ele) for ele in test_list] # printing results print("The mapped List : " + str(res)) |
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']
Method #2 : Using defaultdict() + ascii_lowercase() + iter()
In this we use defaultdict() to assign values to similar elements, ascii_lowercase() is used to get all lowercase all lowercased alphabets.
Python3
# Python3 code to demonstrate working of # Assign Alphabet to each element # Using defaultdict() + ascii_lowercase() + iter() from collections import defaultdict import string # initializing list test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5] # printing list print("The original list : " + str(test_list)) # assigning lowecases as iterator temp = iter(string.ascii_lowercase) # lambda functions fits to similar elements res = defaultdict(lambda: next(temp)) # flatten in list res = [res[key] for key in test_list] # printing results print("The mapped List : " + str(list(res))) |
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']
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.
Recommended Posts:
- Python | Assign ids to each unique value in a list
- Python | Pandas dataframe.assign()
- How to assign values to variables in Python and other languages
- Python | Assign multiple variables with list values
- Python | Assign value to unique number in list
- Python | Assign range of elements to List
- Python - Assign values to initialized dictionary keys
- Python - Assign list items to Dictionary
- Python - Assign K to Non Max-Min elements in Tuple
- Python - Assign Reversed Values in Dictionary
- Python - Assign pair elements from Tuple Lists
- Python - Assign values to Values List
- Python - Assign Frequency to Tuples
- Assign Function to a Variable in Python
- Python | Sort alternate numeric and alphabet list
- Python | First alphabet index
- Python - Index Frequency Alphabet List
- Python IMDbPY – Get each episode name of each season of the series
- Python IMDbPY – Getting each episode year of each season of the series
- Python IMDbPY – Get each episode ID of each season of the series
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.

