Python - Replace list elements with its ordinal number.


In this article we will learn how to replace any given list element with its ordinal number. We will see various methods to get this work done. Before that let’s know about ordinal numbers.

Ordinal number − In mathematics we say ordinal number as number which represents the position or original order of list items present in a sequence.

Take an example of a list: [ 6, 8, 5, 3 ] so ordinal value of these number would be −

  • The first element (6) has ordinal value 1.

  • The second element (8) has ordinal value 2.

  • The third element (5) has ordinal value 3.

  • The fourth element (3) has ordinal value 4.

Let’s look at various methods to replace the list items with an ordinal number.

Method 1. Using a Nested List Comprehension With Enumerate.

Example

List_items = [[6, 8, 5, 3], [22, 52, 13], [11], [35], [61, 15]]
replaced_ordinal_list = [[idx] * len(sublist) for idx, sublist in enumerate(List_items)]
print(replaced_ordinal_list)

Output

[[0, 0, 0, 0], [1, 1, 1], [2], [3], [4, 4]]

Explanation

In the above program we used nested list comprehension. Outer comprehension iterates over every sublist in List_items and inner comprehension generates lists by traversing each inner sublist.

Method 2. Using Nested Loops.

Example

List_items = [[6, 8, 12, 15], [2, 5, 13], [1], [5, 5], [6, 15]]
replaced_ordinal_list = []
for idx, sublist in enumerate(List_items):
   ordinal_list = [idx] * len(sublist)
   replaced_ordinal_list.append(ordinal_list)
print(replaced_ordinal_list)

Output

[[0, 0, 0, 0], [1, 1, 1], [2], [3, 3], [4, 4]]

Explanation

In the above program we used nested for loop for traversing on each sublist

In List_items. While traversing in the outer loop we created ordinal list and multiplied its index by length of each sublist.

Method 3. Using List Comprehension With a While Loop.

Example

List_items= [[6, 8, 12, 15], [2, 5, 13,], [1], [5, 2, 6], [6, 15]]
replaced_ordinal_list= []
idx = 0
while idx < len(List_items):
   ordinal_list = [idx] * len(List_items[idx])
   replaced_ordinal_list.append(ordinal_list)
   idx += 1
print(replaced_ordinal_list)

Output

[[0, 0, 0, 0], [1, 1, 1], [2], [3, 3, 3], [4, 4]]

Explanation

In the above program we used while loop to traverse over each sublist in List_items. While traversing in the outer loop we created an ordinal list

Method 4. Using a Dictionary and Enumerate.

Example

import numpy as np

List_items= [[6, 8, 12, 15], [2, 5, 13,], [1], [5, 2, 6], [6, 15]]
replaced_ordinal_list= [{idx: sublist} for idx, sublist in enumerate(List_items)]
print(replaced_ordinal_list)

Output

[{0: [6, 8, 12, 15]}, {1: [2, 5, 13]}, {2: [1]}, {3: [5, 2, 6]}, {4: [6, 15]}]

Explanation

In the above program we create dictionary for every sublist in List_items. Here index (idx) behaves like key and sublist behaves as value. We store the resultant dictionary in the replaced_ordinal_list.

Method 5. Using List Comprehension With Indexing.

In this method we will look at situation when we want to represent one sublist as index 0 and the next sublist as increment in the index of the previous sublist then how we can do this efficiently.

Example

import numpy as np

List_items= [[6, 8, 12, 15], [2, 5, 13,], [1], [5, 2, 6], [6, 15]]
replaced_ordinal_list = [[idx] for idx, sublist in enumerate(List_items) for _ in sublist]
print(replaced_ordinal_list)

Output

[[0], [0], [0], [0], [1], [1], [1], [2], [3], [3], [3], [4], [4]]

Explanation

In the above program we used list comprehension with nested loops. Outer loop iterates over each sublist in List_items and the inner loop repeats the index for each element in the sublist.

Method 6: Using Recursion

Example

def replace_ordinal_fun(List_items, ind=0):
   if not List_items:
      return []
   return [ind] * len(List_items[0]) + replace_ordinal_fun(List_items[1:], ind+1)

List_items= [[6, 8, 12, 15], [2, 5, 13,], [1], [5, 2, 6], [6, 15]]
replaced_ordinal_list = replace_ordinal_fun(List_items)
print(replaced_ordinal_list)

Output

[0, 0, 0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 4]

Explanation

In the above program we use the concept of recursion function replace_ordinal_fun for replacing the list element with its ordinal number. Here the recursion function takes the input parameter as list of items and index starting from 0. In each recursion it takes sublist and constructs the ordinal number for that list, then in the next recursion call it takes another sublist and again constructs the ordinal number for that list. It will return when the input list will be empty which works as base case for the recursion function.

Method 7: Using a Generator Expression.

Example

import numpy as np

List_items= [[6, 8, 12, 15], [2, 5, 13,], [1], [5, 2, 6], [6, 15]]
replaced_ordinal_list = list([idx] * len(sublist) for idx, sublist in enumerate(List_items))
print(replaced_ordinal_list)

Output

[[0, 0, 0, 0], [1, 1, 1], [2], [3, 3, 3], [4, 4]]

Explanation

In the above program we used generator expression inside list constructor which is used to generate lists by multiplying the index with length of each sublist. After generating the expression, we convert the resultant generator expression into the list named as replaced_ordinal_list.

So, we get to know about multiple methods using which we can replace list elements with its ordinal number. Each of the methods are having ut;s own approach to find the ordinal number. You can choose the methods which are suitable for your requirement but having ideas about various methods is good for learning.

Updated on: 03-Oct-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements