- 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 find Non-K distant elements
When it is required to find non ‘K’ distant elements, a simple iteration along with the ‘append’ method is used.
Below is a demonstration of the same
my_list = [91, 13, 19, 25, 35, 3, 9, 11, 0] print("The list is :") print(my_list) my_key = 2 print("The key is ") print(my_key) my_result =  for element in my_list: if element + my_key not in my_list and element - my_key not in my_list: my_result.append(element) print("The resultant list is :") print(my_result)
The list is : [91, 13, 19, 25, 35, 3, 9, 11, 0] The key is 2 The resultant list is : [91, 19, 25, 35, 3, 0]
A list is defined and is displayed on the console.
The key value is defined and displayed on the console.
An empty list is defined.
The list is iterated over, and the key is checked to see if it is present in the list or not.
If yes, it is appended to the empty list.
The output is displayed on the console.
- Maximum sum subsequence with at-least k distant elements in C++ program
- Maximum sum subsequence with at-least k distant elements in C++
- Program to find k where k elements have value at least k in Python
- Program to find minimum amplitude after deleting K elements in Python
- Distant Barcodes in Python
- Program to find number of sets of k-non-overlapping line segments in Python
- Program to find sum of non-adjacent elements in a circular list in python
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find three unique elements from list whose sum is closest to k Python
- Python - Remove non-increasing elements
- Program to find elements from list which have occurred at least k times in Python
- Program to Find K-Largest Sum Pairs in Python
- Program to find lexicographically smallest non-palindromic string in Python
- Find K Closest Elements in C++
- Python – K middle elements