Open In App

Assign Alphabet to Each Element – Python

Last Updated : 29 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of assigning an alphabet to each element in a list involves transforming the elements of the list into corresponding alphabetic characters, where each unique element is mapped to a unique letter. In this task, the goal is to iterate through a list and assign a letter from the alphabet to each unique element based on its first occurrence.

For example, given a list like li = [4, 5, 2, 4, 2, 6, 5, 2, 5], the goal is to convert it into a list of alphabetic characters where each unique number is assigned a letter e.g. {4: ‘a’, 5: ‘b’, 2: ‘c’, 6: ‘d’} , resulting in the transformed list [‘a’, ‘b’, ‘c’, ‘a’, ‘c’, ‘d’, ‘b’, ‘c’, ‘b’].

Using itertools.count

This method utilizes the itertools.count generator in combination with a defaultdict to assign alphabets dynamically. Count generator increments indices lazily, which means values are only generated when needed.

Python
from collections import defaultdict
from itertools import count
import string

li = [4, 5, 2, 4, 2, 6, 5, 2, 5]

counter = count()  # Step 1
temp = defaultdict(lambda: string.ascii_lowercase[next(counter)])  # Step 2
res = [temp[ele] for ele in li]  # Step 3

print(res)

Output
['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']

Explanation:

  • count()) creates an infinite iterator that will give the next number every time it is called.
  • defaultdict(lambda: string.ascii_lowercase[next(counter)]) creates a dictionary that assigns a letter from the alphabet to each unique element based on the counter’s value.
  • [temp[ele] for ele in li] uses a list comprehension to map the elements in li to the corresponding letters, based on their first occurrence in the list.

Using enumerate

Set extract unique elements from the list and enumerate assign sequential alphabet indices. It ensures a deterministic mapping by sorting the unique elements before indexing.

Python
import string

li = [4, 5, 2, 4, 2, 6, 5, 2, 5]

unique_elements = {ele: string.ascii_lowercase[i] for i, ele in enumerate(sorted(set(li)))}
res = [unique_elements[ele] for ele in li]

print(res)

Output
['b', 'c', 'a', 'b', 'a', 'd', 'c', 'a', 'c']

Explanation:

  • {ele: string.ascii_lowercase[i] for i, ele in enumerate(sorted(set(li)))} converts the list li to a set to remove duplicates, sorts the set and then creates a dictionary that maps each unique element to an alphabet letter .
  • List comprehension iterates over the original list li and replaces each element with the corresponding letter from the unique_elements dictionary.

Using dictionary

A straightforward method where a dictionary is used to manually track indices and map elements to alphabets. It iterates through the list and assigns alphabets when new elements are encountered.

Python
import string

li = [4, 5, 2, 4, 2, 6, 5, 2, 5]

temp = {}  # Initialize an empty dictionary
index = 0   # Initialize the index

for ele in li:  # Iterate over the list `li`
    if ele not in temp:
        temp[ele] = string.ascii_lowercase[index]
        index += 1

res = [temp[ele] for ele in li]
print(res)

Output
['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']

Explanation:

  • if ele not in temp checks if the element is already assigned an alphabet.
  • temp[ele] = string.ascii_lowercase[index] assigns the letter corresponding to the current index from string.ascii_lowercase .
  • index += 1 increments the index to point to the next letter for the next unique element.
  • res = [temp[ele] for ele in li] creates a new list res by mapping each element in li to its assigned alphabet from temp.

Using list comprehension

list comprehension efficiently assign an alphabet to each element in a list. The method combines dictionary operations with conditional checks in a compact and readable one-liner. By using temp.setdefault(), we ensure that each unique element is mapped to an alphabet letter and the index is incremented only when a new element is encountered.

Python
import string

li = [4, 5, 2, 4, 2, 6, 5, 2, 5]

temp = {} # Initialize an empty dictionary
index = 0   # Initialize the index

res = [
    temp.setdefault(ele, string.ascii_lowercase[index := index + 1 if ele not in temp else index])
    for ele in li
]
print(res)

Output
['b', 'c', 'd', 'b', 'd', 'e', 'c', 'd', 'c']

Explanation:

  • temp.setdefault(ele, …) for each element in li, it checks if it’s in temp. If not, it assigns an alphabet letter based on the current index.
  • string.ascii_lowercase[index := index + 1 if ele not in temp else index] assigns a letter from string.ascii_lowercase and increments the index only when a new element is encountered.


Next Article

Similar Reads