- 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 – K middle elements
When it is required to determine K middle elements, the ‘//’ operator and list slicing is used.
Below is a demonstration of the same −
my_list = [34, 56, 12, 67, 88, 99, 0, 1, 21, 11] print("The list is : ") print(my_list) K = 5 print("The value of K is ") print(K) beg_indx = (len(my_list) // 2) - (K // 2) end_indx = (len(my_list) // 2) + (K // 2) my_result = my_list[beg_indx: end_indx + 1] print("The result is : " ) print(my_result)
The list is : [34, 56, 12, 67, 88, 99, 0, 1, 21, 11] The value of K is 5 The result is : [67, 88, 99, 0, 1]
A list is defined and is displayed on the console.
A value for K is defined and is displayed on the console.
The length of the list is obtained and the ‘//’ operator is used.
The difference between above value and K//2 is assigned to a variable.
The sum of these two values is also assigned to a different variable.
A list slicing operation is done to access specific elements.
This is assigned to a variable.
This is displayed as the output on the console.
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Top K Frequent Elements in Python
- Python – Reform K digit elements
- Program to find k where k elements have value at least k in Python
- Delete elements with frequency atmost K in Python
- Extract tuples having K digit elements in Python
- Python – Next N elements from K value
- Python – Random insertion of elements K times
- Python program to find Non-K distant elements
- Maximum and Minimum K elements in Tuple using Python
- Python – Remove Elements in K distance with N
- Python – Elements with factors count less than K
- Adding elements in the middle of an ArrayList in Java
- Add elements at the middle of a Vector in Java