Python - Replace list elements with its ordinal number.

In this article, we will learn how to replace list elements with their ordinal numbers (position-based indices). An ordinal number represents the position of an item in a sequence, starting from 0 in programming.

For example, in the list [6, 8, 5, 3], the ordinal values are:

  • Element 6 has ordinal value 0 (first position)

  • Element 8 has ordinal value 1 (second position)

  • Element 5 has ordinal value 2 (third position)

  • Element 3 has ordinal value 3 (fourth position)

Let's explore various methods to replace nested list elements with their ordinal numbers.

Using List Comprehension with Enumerate

This method uses nested list comprehension to replace each sublist with ordinal numbers ?

nested_list = [[6, 8, 5, 3], [22, 52, 13], [11], [35], [61, 15]]
ordinal_result = [[idx] * len(sublist) for idx, sublist in enumerate(nested_list)]
print(ordinal_result)
[[0, 0, 0, 0], [1, 1, 1], [2], [3], [4, 4]]

Using Nested For Loops

A more explicit approach using traditional loops ?

nested_list = [[6, 8, 12, 15], [2, 5, 13], [1], [5, 5], [6, 15]]
ordinal_result = []

for idx, sublist in enumerate(nested_list):
    ordinal_sublist = [idx] * len(sublist)
    ordinal_result.append(ordinal_sublist)

print(ordinal_result)
[[0, 0, 0, 0], [1, 1, 1], [2], [3, 3], [4, 4]]

Using While Loop

Alternative approach with a while loop for manual index control ?

nested_list = [[6, 8, 12, 15], [2, 5, 13], [1], [5, 2, 6], [6, 15]]
ordinal_result = []
idx = 0

while idx < len(nested_list):
    ordinal_sublist = [idx] * len(nested_list[idx])
    ordinal_result.append(ordinal_sublist)
    idx += 1

print(ordinal_result)
[[0, 0, 0, 0], [1, 1, 1], [2], [3, 3, 3], [4, 4]]

Flattening with Ordinal Values

This method creates a flat list where each element is replaced by its sublist's ordinal number ?

nested_list = [[6, 8, 12, 15], [2, 5, 13], [1], [5, 2, 6], [6, 15]]
flat_ordinal = [idx for idx, sublist in enumerate(nested_list) for _ in sublist]
print(flat_ordinal)
[0, 0, 0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 4]

Using Recursion

A recursive approach that processes sublists one by one ?

def replace_with_ordinal(nested_list, index=0):
    if not nested_list:
        return []
    return [index] * len(nested_list[0]) + replace_with_ordinal(nested_list[1:], index + 1)

nested_list = [[6, 8, 12, 15], [2, 5, 13], [1], [5, 2, 6], [6, 15]]
flat_ordinal = replace_with_ordinal(nested_list)
print(flat_ordinal)
[0, 0, 0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 4]

Comparison of Methods

Method Output Format Best For
List Comprehension Nested lists Concise, readable code
For Loops Nested lists Beginners, debugging
Flattening Flat list When structure doesn't matter
Recursion Flat list Functional programming style

Conclusion

Use list comprehension for clean, readable code when preserving nested structure. Choose flattening methods when you need a simple flat list with ordinal values replacing original elements.

---
Updated on: 2026-03-27T14:37:56+05:30

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements