Skip to content
Related Articles

Related Articles

Improve Article

Python | Replace list elements with its ordinal number

  • Difficulty Level : Medium
  • Last Updated : 28 Feb, 2019

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.




# 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))
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.




# 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))
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.




# 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))
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.




# 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))
Output:
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :