- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to replace each element by smallest term at left side in Python

Suppose we have a list of numbers called nums, we have to replace every nums[i] with the smallest element left of i. We have to replace nums[0] with 0.

So, if the input is like [15, 7, 9, 16, 12, 25], then the output will be [0, 15, 7, 7, 7, 7]

To solve this, we will follow these steps −

- if nums is empty, then
- return a new list

- j:= nums[0]
- nums[0]:= 0
- for i in range 1 to size of nums - 1, do
- k:= nums[i]
- nums[i]:= j
- j:= minimum of j, k

- return nums

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): if not nums: return [] j=nums[0] nums[0]=0 for i in range(1,len(nums)): k=nums[i] nums[i]=j j=min(j,k) return nums ob = Solution() nums = [15, 7, 9, 16, 12, 25] print(ob.solve(nums))

## Input

[15, 7, 9, 16, 12, 25]

## Output

[0, 15, 7, 7, 7, 7]

- Related Articles
- Program to find smallest intersecting element of each row in a matrix in Python
- JAVA Program to Replace Each Element of Array with its Next Element
- Program to find kth smallest element in linear time in Python
- Replace Elements with Greatest Element on Right Side in C++
- Python program to replace first ‘K’ elements by ‘N’
- Python program to find k'th smallest element in a 2D array
- Program to find the kth smallest element in a Binary Search Tree in Python
- Program to find size of smallest sublist whose sum at least target in Python
- Python Program to Select the nth Smallest Element from a List in Expected Linear Time
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python Program to find the cube of each list element
- Program to find replicated list by replicating each element n times
- Kth Smallest Element in a BST in Python
- C++ Program to find kth Smallest Element by the Method of Partitioning the Array\n
- Create a linked list from two linked lists by choosing max element at each position in C++ Program

Advertisements