Python | Replace list elements with its ordinal number
Last Updated :
08 Aug, 2024
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.
Python
# 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]]
Time Complexity: O(n), where n is the length of the list t
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
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.
Python
# 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.
Python
# 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.
Python
# 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]]
Approach#5: In this approach, we will iterate over each element of the list and replace it with its corresponding index value. We will use a nested for loop to traverse the list.
Algorithm
1. Traverse the list using a nested for loop.
2. Replace each element of the list with its index value.
Python
def replace_with_ordinal(lst):
for i in range(len(lst)):
for j in range(len(lst[i])):
lst[i][j] = i
return lst
lst=[[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replace_with_ordinal(lst))
Output[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time Complexity: O(n^2)
Auxiliary Space: O(1)
METHOD 6:Using def and len.
APPROACH:
This algorithm is implemented in the replace_with_ordinal function, which returns a new list with each element replaced with its corresponding index in the input list.
ALGORITHM:
1. Define a function named `replace_with_ordinal` that takes an input list as argument.
2. Create an empty list called `result`.
3. Loop through each list `lst` in the input list using a `for` loop.
4. Create an empty list called `ordinal_list`.
5. Loop through each element `elem` in `lst` using a `for` loop.
6. Append the index of `lst` to `ordinal_list` for each element `elem`.
7. Append `ordinal_list` to `result`.
8. Return `result`.
Python
def replace_with_ordinal(input_list):
return [[i for _ in lst] for i, lst in enumerate(input_list)]
input_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
output_list = replace_with_ordinal(input_list)
print(output_list)
Output[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time complexity:
The time complexity of this implementation is O(n^2), where n is the number of elements in the input list.
Space complexity:
The space complexity of this implementation is also O(n^2), where n is the number of elements in the input list.
Approach#7: In this approach, we can use the map() function along with a lambda function to replace the elements with their ordinal numbers. The lambda function takes two arguments, index and the sublist, and returns the index value for each element of the sublist.
Algorithm:
Define a lambda function that takes two arguments, index and sublist, and returns the index value for each element of the sublist.
Use the map() function to apply the lambda function to each element of the list.
Python
def replace_with_ordinal(lst):
return list(map(lambda x: list(map(lambda y: x[0], x[1])), enumerate(lst)))
lst = [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]
print(replace_with_ordinal(lst))
Output[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time Complexity: O(n^2) (due to the nested maps)
Auxiliary Space: O(n) (due to the creation of a new list)
Similar Reads
Python Program to replace list elements within a range with a given number
Given a range, the task here is to write a python program that can update the list elements falling under a given index range with a specified number. Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], i, j = 4, 8, K = 9 Output : [4, 6, 8, 1, 9, 9, 9, 9, 12, 3, 9, 1] Explanation : List is u
3 min read
Replace index elements with elements in Other List-Python
The task of replacing index elements with elements from another list involves mapping the indices from one list to the corresponding elements in a second list. For each index in the first list, the element at that index is retrieved from the second list and stored in a new result list. For example,
4 min read
Python program to replace first 'K' elements by 'N'
Given a List, replace first K elements by N. Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 4, N = 3 Output : [3, 3, 3, 3, 4, 2, 6, 9] Explanation : First 4 elements are replaced by 3. Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 2, N = 10 Output : [10, 10, 6, 8, 4, 2, 6, 9] Explanation : Fi
4 min read
Python - Replace Elements greater than K
Given element list, replace all elements greater than K with given replace character. Input : test_list = [3, 4, 7, 5, 6, 7], K = 5, repl_chr = None Output : [3, 4, None, 5, None, None] Explanation : Characters are replaced by None, greater than 5. Input : test_list = [3, 4, 7, 5, 6, 7], K = 4, repl
4 min read
Python - Append given number with every element of the list
In Python, lists are flexible data structures that allow us to store multiple values in a single variable. Often, we may encounter situations where we need to modify each element of a list by appending a given number to it. Given a list and a number, write a Python program to append the number with
5 min read
Python - Replace sublist from Initial element
Given a list and replacement sublist, perform the replacement of list sublist on basis of initial element of replacement sublist. Input : test_list = [3, 7, 5, 3], repl_list = [7, 8] Output : [3, 7, 8, 3] Explanation : Replacement starts at 7 hence 7 and 8 are replaced. Input : test_list = [3, 7, 5,
7 min read
Python - Odd elements removal in List
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
5 min read
Python Program to replace elements of a list based on the comparison with a number
Given a list, the task here is to write a Python program to replace its elements after comparing them with another number here described using K. For the example depicted in this article, any number greater than K will be replaced with the value given in high and any number less than or equal to K w
4 min read
Python | Finding relative order of elements in list
Sometimes we have an unsorted list and we wish to find the actual position the elements could be when they would be sorted, i.e we wish to construct the list which could give the position to each element destined if the list was sorted. This has a good application in web development and competitive
3 min read
Python - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9] Output : [1, 4, 6] Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9] Output : [] Explanation : No number at its index. Method #1:
5 min read