- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to replace first ‘K’ elements by ‘N’
When it is required to replace first ‘K’ elements by ‘N’, a simple iteration is used.
Below is a demonstration of the same −
my_list = [13, 34, 26, 58, 14, 32, 16, 89] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 99 print("The value of N is :") print(N) for index in range(K): my_list[index] = N print("The result is :") print(my_list)
The list is : [13, 34, 26, 58, 14, 32, 16, 89] The value of K is : 2 The value of N is : 99 The result is : [99, 99, 26, 58, 14, 32, 16, 89]
A list of integers is defined and is displayed on the console.
The value for ‘K’ and ‘N’ are defined and displayed on the console.
The range within ‘K’ is iterated over, and every element in the index is assigned the value of N.
This is done up to the values within index ‘K’.
- Python Program to Reverse only First N Elements of a Linked List
- Python – Next N elements from K value
- Trim tuples by N elements in Python
- Python – Remove Elements in K distance with N
- Python program to find Non-K distant elements
- Python Program to Get K initial powers of N
- Program to generate first n lexicographic numbers in python
- How to group Python tuple elements by their first element?
- Program to find k where k elements have value at least k in Python
- Python program to randomly create N Lists of K size
- Print first k digits of 1/n where n is a positive integer in C Program
- Python - Merge a Matrix by the Elements of First Column
- Python program to find N-sized substrings with K distinct characters
- Python program to right rotate a list by n
- Python Program to Group Strings by K length Using Suffix