Python - Check if elements index are equal for list elements
Last Updated :
10 Mar, 2023
Given two lists and check list, test if for each element in check list, elements occur in similar index in 2 lists.
Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7] Output : False Explanation : 7 is at 4th and 2nd place in both list, hence False. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 6, 9, 4, 8], check_list = [9, 8, 6] Output : True Explanation : All from checklist at similar index, hence True.
Method #1 : Using loop
In this, we iterate for all the elements in list, if elements are different and is present in check list, then False is returned.
Python3
# Python3 code to demonstrate working of
# Check if elements index are equal for list elements
# Using loop
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
res = True
for idx, ele in enumerate(test_list1):
# check for indifference and containment
if test_list1[idx] != test_list2[idx] and ele in check_list:
res = False
break
# printing result
print("Are elements at same index for required instances ?: " + str(res))
OutputThe original list 1 : [2, 6, 9, 7, 8]
The original list 2 : [2, 7, 9, 4, 8]
Are elements at same index for required instances ?: True
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using zip() + all() + generator expression
In this, we pair like indices using zip() and then all() is used to check for all indices, generator expression is used for iteration logic.
Python3
# Python3 code to demonstrate working of
# Check if elements index are equal for list elements
# Using zip() + all() + generator expression
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
# checking for all elements equal in check list using all()
res = all(a == b for a, b in zip(test_list1, test_list2) if a in check_list)
# printing result
print("Are elements at same index for required instances ?: " + str(res))
OutputThe original list 1 : [2, 6, 9, 7, 8]
The original list 2 : [2, 7, 9, 4, 8]
Are elements at same index for required instances ?: True
Method#3:Using list comprehension
Python3
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
# Using a list comprehension and the all function
# all returns True if all elements in the iterable are True, otherwise False
res = all(test_list1[i] == test_list2[i] for i in range(len(test_list1)) if test_list1[i] in check_list)
# printing result
print("Are elements at same index for required instances ?: " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list 1 : [2, 6, 9, 7, 8]
The original list 2 : [2, 7, 9, 4, 8]
Are elements at same index for required instances ?: True
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Check if all elements in a list are identical Checking if all elements in a list are identical means verifying that every item has the same value. It's useful for ensuring data consistency and can be done easily using Python's built-in features.Examples:Input: ['a', 'b', 'c']Output: FalseInput: [1, 1, 1, 1]Output: TrueLet's explore different me
3 min read
Python | Check if all elements in a list are identical Checking if all elements in a list are identical means verifying that every item has the same value. It's useful for ensuring data consistency and can be done easily using Python's built-in features.Examples:Input: ['a', 'b', 'c']Output: FalseInput: [1, 1, 1, 1]Output: TrueLet's explore different me
3 min read
Python | Check if all elements in list follow a condition Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using
4 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python | Check for Nth index existence in list Sometimes, while working with lists, we can have a problem in which we require to insert a particular element at an index. But, before that it is essential to know that particular index is part of list or not. Let's discuss certain shorthands that can perform this task error free. Method #1 : Using
3 min read