Python | Replace list elements with its ordinal number

Given a list of lists, write a Python program to replace the values in the inner lists with their ordinal values.

Examples:

Input : [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
Output : [[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]

Input : [['a'], [ 'd', 'e', 'b', 't'], [ 'x', 'l']]
Output : [[0], [1, 1, 1, 1], [2, 2]]

 
Approach #1 : Naive Approach

This method is a one-liner Naive approach in which we make use of two for loops using i and j variable and iterate through each inner list to replace it with the ith ordinal number.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Replace element
# in a list with its ordinal number 
  
def replaceOrdinal(lst):
    return [[i for j in range(len(lst[i]))] 
    for i in range(len(lst))]
      
# Driver Code
lst = [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replaceOrdinal(lst))

chevron_right


Output:

[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]

 
Approach #2 : Pythonic Naive

This is another naive approach, but more pythonic. For every inner list, it returns the ith position (which is its ordinal number) and then multiplies it with the length of that particular inner list in order to return the desired output.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Replace element
# in a list with its ordinal number 
  
def replaceOrdinal(lst):
    return [[i]*len(lst[i]) for i in range(len(lst))]
      
# Driver Code
lst = [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replaceOrdinal(lst))

chevron_right


Output:

[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]

 
Approach #3 : Using Python enumerate() method
We can use list comprehension along with Python enumerate(). This method adds a counter to an iterable and returns it in a form of enumerate object. This counter index will be used as the ordinal number. And thus, we return the respective counter index for every element of inner sublist.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Replace element
# in a list with its ordinal number 
  
def replaceOrdinal(lst):
    return [[idx for _ in sublist] 
    for idx, sublist in enumerate(lst)]
      
# Driver Code
lst = [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replaceOrdinal(lst))

chevron_right


Output:

[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]

 
Approach #4 : Alternative to use enumerate()
Here, we use Python enumerate() in a similar method as in above method, but instead of another loop, we follow the approach #4 to produce the inner list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Replace element
# in a list with its ordinal number 
  
def replaceOrdinal(lst):
    return [[index] * len(sublist) for index,
    sublist in enumerate(lst)]
      
# Driver Code
lst = [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replaceOrdinal(lst))

chevron_right


Output:

[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]


My Personal Notes arrow_drop_up


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.