

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 – Next N elements from K value
When it is required to get next N elements from K value, a simple iteration is used.
Below is a demonstration of the same −
Example
my_list = [31, 24, 46, 18, 34, 52, 26, 29] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 3 print("The value of N is :") print(N) for index in range(K): my_list[index] = N print("The result is :") print(my_list)
Output
The list is : [31, 24, 46, 18, 34, 52, 26, 29] The value of K is : 2 The value of N is : 3 The result is : [3, 3, 46, 18, 34, 52, 26, 29]
Explanation
A list is defined and displayed on the console.
The values for K and N are defined and displayed on the console.
The list is iterated over in the range of K, and the value of N is assigned the element at specific indices.
This is the output that is displayed on the console.
- Related Questions & Answers
- Python – Remove Elements in K distance with N
- Python – K middle elements
- Python – Reform K digit elements
- Python – Split List on next larger value
- Python – Random insertion of elements K times
- Python – N sized substrings with K distinct characters
- Python – Elements with factors count less than K
- Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++
- Python – Extract Rear K digits from Numbers
- Program to find k where k elements have value at least k in Python
- Python – Filter rows with Elements as Multiple of K
- Python – Extract elements with equal frequency as value
- Python – Extract String elements from Mixed Matrix
- Python – Extract elements from Ranges in List
- Python – Extract element from a list succeeded by K
Advertisements