When it is required to remove duplicate tuples from a list of tuples, the loop, the 'any' method and the enumerate method can be used.The 'any' method checks to see if any value in the iterable is True, i.e atleast one single value is True. If yes, it returns 'True', else returns 'False'A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The enumerate method adds a counter to the given iterable, and returns it.Below is a demonstration ... Read More
When it is required to initialize the tuples with certain parameters, the 'tuple' method and the '*' operator can be used.The 'tuple' method will convert the iterable passed to it as parameter into a tuple class type.The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.Below is a demonstration for the same −ExampleLive DemoN = 6 print("The value of N has been initialized to "+str(N)) default_val = 2 print("The default value has been initialized to " +str(default_val)) ... Read More
When it is required to convert a tuple into an integer, the lambda function and the 'reduce' function can be used.Anonymous function is a function which is defined without a name. The reduce function takes two parameters- a function and a sequence, where it applies the function to all the elements of the list/sequence. It is present in the 'functools' module.In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the ... Read More
When it is required to implement shell sort, a function is defined, and this takes a list and the length of the list as arguments. This list is sorted up to a specific number of elements, wherein the number of elements is the largest value. This is done until the number of elements has the smallest value.This is done for all sub-lists in the list, and all these sub-lists are sorted.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration of the same −ExampleLive ... Read More
When it is required to sort the lists in a tuple, the 'tuple' method, the 'sorted' method and a generator expression can be used.The 'sorted' method is used to sort the elements of a list. It is a built-in function, that returns the sorted list.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.The 'tuple' method takes an iterable as argument, and converts it into a tuple type.A list can ... Read More
When it is required to sort the list of tuples in a customized manner, the 'sort' method can be used.The 'sort' method sorts the elements of the iterable in a specific order, i.e ascending or descending. It sorts the iterable in-place.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.Below is a demonstration of the same −ExampleLive Demodef tuple_sort(my_tup): my_tup.sort(key = lambda x: x[1]) return my_tup my_tuple = [('Will', 100), ('John', 67), ('Harold', 86), ... Read More
When it is required to get the tuple element of data types, the 'map' method and the 'type' method can be used.The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.The 'type' method returns the type of class of the argument that is passed to it.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration of the same −ExampleLive Demomy_tuple = ('Hi', 23, ['there', 'Will']) print("The tuple is : ... Read More
When it is required to check the order of a specific data type in a tuple, the 'isinstance' method and the 'chained if' can be used.The 'isinstance' method checks to see if a given parameter belong to a specific data type or not.The 'chained if' is a chained conditional statement. It is a different way of writing nested selection statements. It basically means multiple if statements are combined using 'and' operator, and their result is evaluated.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a ... Read More
When it is required to convert the location co-ordinates into a tuple format, the 'eval' method can be used.The 'eval' method parses the expression which is passed to it as an argument. It executes that arguments as the code. It returns the result which is evaluated from the 'expression', i.e the parameter.Below is a demonstration of the same −ExampleLive Demomy_string = "67.5378, -78.8523" print("The string is : ") print(my_string) my_result = eval(my_string) print("The coordinates after converting the string to tuple is : ") print(my_result)OutputThe string is : 67.5378, -78.8523 The coordinates after converting the string to tuple ... Read More
When it is required to remove the duplicates present in tuple of list, as well as preserving the order, a list comprehension and the 'set' method can be used.The list comprehension is a shorthand to iterate through the list and perform operations on it.Python comes with a datatype known as 'set'. This 'set' contains elements that are unique only. The set is useful in performing operations such as intersection, difference, union and symmetric difference.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ([1, 21, 34] , [11, 0, 98], [45, 67, 56]) print("The tuple of list is : ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP