- 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
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 Articles
- Python – Remove Elements in K distance with N
- Python program to replace first ‘K’ elements by ‘N’
- Program to find k where k elements have value at least k in Python
- Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python
- Python – K middle elements
- Get last N elements from given list in Python
- Interesting Python Implementation for Next Greater Elements
- Python – Reform K digit elements
- Python program to find N largest elements from a list
- Python – Split List on next larger value
- Program to create a list with n elements from 1 to n in Python
- In an AP, if ( S_{n}=3 n^{2}+5 n ) and ( a_{k}=164 ), find the value of ( k ).
- Find top K frequent elements from a list of tuples in Python
- Top K Frequent Elements in Python
- Maximum value K such that array has at-least K elements that are >= K in C++

Advertisements