Wednesday, June 10, 2026
HomeLanguagesPython program to test for Non-neighbours in List

Python program to test for Non-neighbours in List

Given two elements, write a Python program to check that they don’t occur as their neighbors in the list.

Examples:

Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 7, 4 
Output : True 
Explanation : 7 doesn't occur are 4's neighbour.
Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 5, 4 
Output : False 
Explanation : 5 occurs are 4's neighbour.

Method #1: Using loop

In this, we check for next and previous elements to not be ‘i’ or ‘j’ while iteration of list. 

Python3




# Python3 code to demonstrate working of
# Test for Non-neighbours in List
# Using loop
 
# Initializing list
test_list = [3, 7, 2, 1, 4, 5, 7, 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing i, j
i, j = 7, 4
 
res = True
for idx in range(1, len(test_list) - 1):
    if test_list[idx] == i:
 
        # check for surrounding element to be j if i
        if test_list[idx - 1] == j or test_list[idx + 1] == j:
            res = False
             
            break
 
# Printing result
print("Are i, j Non-neighbours' : " + str(res))


Output

The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using all() function

This is a one-liner approach to solving this problem. In this, we perform the task of checking for all elements for neighbors using all() in the generator expression in all() functions.

Python3




# Python3 code to demonstrate working of
# Test for Non-neighbours in List
# Using all()
 
# initializing list
test_list = [3, 7, 2, 1, 4, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 7, 4
 
# checking for preceding and succeeding element
# not to be j if curr is i
res = all(test_list[idx - 1] != j and test_list[idx + 1] !=
          j for idx in range(1, len(test_list) - 1) if test_list[idx] == i)
 
# printing result
print("Are i, j Non-neighbours' : " + str(res))


Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using find() method

Python3




# Python3 code to demonstrate working of
# Test for Non-neighbours in List
 
# Initializing list
test_list = [3, 7, 2, 1, 4, 5, 7, 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing i, j
i, j = 7, 4
 
x=list(map(str,test_list))
y="*".join(x)
 
a=str(i)+"*"+str(j)
b=str(j)+"*"+str(i)
 
res = True
 
if y.find(a)!=-1 or y.find(b)!=-1:
    res=False
     
# Printing result
print("Are i, j Non-neighbours' : " + str(res))


Output

The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True

Time Complexity: O(N), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(N), where n is the number of elements in the list “test_list”.

Method 4: Using slicing and set operations

Steps:

  1. Find the indices of i and j in the list using the index() method.
  2. Create two slices of the list:
    • Slice from the start to the index of i.
    • Slice from the index of j to the end.
  3. Create a set by concatenating the two slices and check if j is present in the set.
  4. If j is not present in the set, then return True else return False.

Python3




# Python3 code to demonstrate working of
# Test for Non-neighbours in List
# Using slicing and set operations
 
# Initializing list
test_list = [3, 7, 2, 1, 4, 5, 7, 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing i, j
i, j = 7, 4
 
# Finding indices of i and j
i_idx = test_list.index(i)
j_idx = test_list.index(j)
 
# Creaing two slices
slice1 = set(test_list[:i_idx])
slice2 = set(test_list[j_idx+1:])
 
# check if j is not present in the set
res = j not in slice1.union(slice2)
 
# printing result
print("Are i, j Non-neighbours' : " + str(res))


Output

The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True

Time complexity: O(N)
Auxiliary space: O(N)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS