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
# Python3 code to demonstrate working of # Check if tuple and list are identical # Using loop # Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # printing original list and tuple print ("The original list is : " + str (test_list)) print ("The original tuple is : " + str (test_tup)) # Check if tuple and list are identical # Using loop res = True for i in range ( 0 , len (test_list)): if (test_list[i] ! = test_tup[i]): res = False break # printing result print ("Are tuple and list identical ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple and list are identical # Using all() + zip() # Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # printing original list and tuple print ("The original list is : " + str (test_list)) print ("The original tuple is : " + str (test_tup)) # Check if tuple and list are identical # Using all() + zip() res = all ( [i = = j for i, j in zip (test_list, test_tup)] ) # printing result print ("Are tuple and list identical ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple and list are identical # Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # printing original list and tuple print ( "The original list is : " + str (test_list)) print ( "The original tuple is : " + str (test_tup)) res = False # Check if tuple and list are identical x = list (test_tup) if (test_list = = x): res = True # printing result print ( "Are tuple and list identical ? : " + str (res)) |
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
# Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # printing original list and tuple print ( "The original list is : " + str (test_list)) print ( "The original tuple is : " + str (test_tup)) # Check if tuple and list are identical res = tuple (test_list) = = test_tup # printing result print ( "Are tuple and list identical ? : " + str (res)) #This code is contributed Vinay Pinjala. |
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
# Python code to demonstrate checking if tuple and list are identical # Using all() and lambda function # Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # printing original list and tuple print ( "The original list is : " + str (test_list)) print ( "The original tuple is : " + str (test_tup)) # Check if tuple and list are identical # Using all() and lambda function with zip function res = all ( map ( lambda pair: pair[ 0 ] = = pair[ 1 ], zip (test_list, test_tup))) # printing result print ( "Are tuple and list identical? : " + str (res)) #This code is contributed by Jyothi pinjala. |
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 # Initializing list and tuple test_list = [ 'gfg' , 'is' , 'best' ] test_tup = ( 'gfg' , 'is' , 'best' ) # Convert list to NumPy array list_arr = np.array(test_list) # Convert tuple to NumPy array tup_arr = np.array(test_tup) # Check if NumPy arrays are equal res = np.array_equal(list_arr, tup_arr) # printing original list and tuple print ( "The original list is : " + str (test_list)) print ( "The original tuple is : " + str (test_tup)) # Print the result 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.