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))
|
OutputThe 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))
|
OutputThe 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))
|
OutputThe 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.
Similar Reads
Python | Check if two list of tuples are identical
Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the f
5 min read
Python - Check if tuple list has all K
Sometimes, while working with Python records, we can have a problem in which we need to test if all the elements in tuples of tuple list are K. This problem can have applications in many data domains such as Machine Learning and Web development. Let's discuss certain ways in which this task can be p
7 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. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
3 min read
Python Program To Check If Two Linked Lists Are Identical
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE
4 min read
Python - Check if any list element is present in Tuple
Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements.
6 min read
Python | Arrange Tuples consecutively in list
Sometimes, while working with tuple list, we may require a case in which we require that a tuple starts from the end of previous tuple, i.e the element 0 of every tuple should be equal to ending element of tuple in list of tuple. This type of problem and sorting is useful in competitive programming.
3 min read
Python - Check if Splits are equal
Given String separated by delim, check if all splits are similar. Input : '45#45#45', delim = '#' Output : True Explanation : All equal to 45. Input : '4@5@5', delim = '@' Output : False Explanation : 1st segment equal to 4, rest 5. Method #1 : Using set() + len() + split() In this, we perform split
2 min read
Python - Check if element is present in tuple
We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True. Using in Operatorin operator checks if an element is present in a tuple by
2 min read
Python | Convert Integral list to tuple list
Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read