Python | Check if tuple and list are identical
Last Updated :
04 May, 2023
Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop This is the brute force method to perform this particular task. In this, we just iterate for list and tuple to test if at each index the elements are similar.
Python3
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
print ("The original list is : " + str (test_list))
print ("The original tuple is : " + str (test_tup))
res = True
for i in range ( 0 , len (test_list)):
if (test_list[i] ! = test_tup[i]):
res = False
break
print ("Are tuple and list identical ? : " + str (res))
|
Output :
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical ? : True
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. loop performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using all() + zip() This task can also be performed in a single line using combination of above functions. In this, we just combine the container elements using zip(), and use all() to test for equality of elements at corresponding index.
Python3
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
print ("The original list is : " + str (test_list))
print ("The original tuple is : " + str (test_tup))
res = all ( [i = = j for i, j in zip (test_list, test_tup)] )
print ("Are tuple and list identical ? : " + str (res))
|
Output :
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical ? : True
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using list() method
Python3
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
print ( "The original list is : " + str (test_list))
print ( "The original tuple is : " + str (test_tup))
res = False
x = list (test_tup)
if (test_list = = x):
res = True
print ( "Are tuple and list identical ? : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical ? : True
Method#4: using tuple() method
Python3
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
print ( "The original list is : " + str (test_list))
print ( "The original tuple is : " + str (test_tup))
res = tuple (test_list) = = test_tup
print ( "Are tuple and list identical ? : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: using all() and lambda function :
This code checks whether a list and a tuple are identical in Python. It does so by first initializing a list and a tuple with the same values, and then using the all() function and a lambda function with the zip() function to compare the elements of the two sequences. The zip() function takes two or more iterables and returns an iterator of tuples that aggregates their elements. The lambda function checks if the elements in each corresponding tuple are equal, and the all() function returns True if all elements in the resulting sequence are True.
In essence, this code uses built-in Python functions to compare a list and a tuple element-wise and determine if they have the same values in the same order. If all elements are equal, the code outputs True. Otherwise, it outputs False.
Python3
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
print ( "The original list is : " + str (test_list))
print ( "The original tuple is : " + str (test_tup))
res = all ( map ( lambda pair: pair[ 0 ] = = pair[ 1 ], zip (test_list, test_tup)))
print ( "Are tuple and list identical? : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical? : True
# Time complexity: O(n), where n is the length of the shortest sequence (list or tuple) because all() and zip() functions take linear time to process the inputs.
# Auxiliary Space: O(1) because no extra space is used, only temporary variables are created inside the lambda function.
Method#5: Using numpy:
Algorithm:
- Create a NumPy array from the given list and tuple.
- Compare the NumPy array of the list with the NumPy array of the tuple using the np.array_equal() method.
- If the arrays are equal, print “Are tuple and list identical? : True”, else print “Are tuple and list identical? : False”.
Python3
import numpy as np
test_list = [ 'gfg' , 'is' , 'best' ]
test_tup = ( 'gfg' , 'is' , 'best' )
list_arr = np.array(test_list)
tup_arr = np.array(test_tup)
res = np.array_equal(list_arr, tup_arr)
print ( "The original list is : " + str (test_list))
print ( "The original tuple is : " + str (test_tup))
print ( "Are tuple and list identical? :" , res)
|
Output:
The original list is : ['gfg', 'is', 'best']
The original tuple is : ('gfg', 'is', 'best')
Are tuple and list identical? : True
Time Complexity:
The time complexity is O(n), where n is the number of elements in the input list or tuple. Creating the NumPy arrays, comparing them, and printing the output all take constant time for this particular use case.
Space Complexity:
The space complexity is O(n), where n is the number of elements in the input list or tuple. This is because we create a NumPy array from the input list and tuple, which requires additional memory to store. However, this additional memory usage is negligible for small inputs.
Share your thoughts in the comments
Please Login to comment...