Modulo of Tuple Elements in Python

AmitDiwan
Updated on 11-Mar-2021 09:21:52

657 Views

If the modulo of tuple elements is required to be determined, the 'zip' method and a generator expression can be used.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 zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ( 67, 45, 34, 56) my_tuple_2 = (99, 123, 10, 56) print ("The first tuple ... Read More

Sort a Dictionary in Python

pawandeep
Updated on 11-Mar-2021 09:21:11

5K+ Views

A dictionary is a data structure that consists of key and value pairs. We can sort a dictionary using two criteria −Sort by key − The dictionary is sorted in ascending order of its keys. The values are not taken care of.Sort by value − The dictionary is sorted in ascending order of the values.Method 1 − Sort the dictionary by keyIn this approach, the dictionary is sorted in ascending order of its keys.Input:{2:90, 1: 100, 8: 3, 5: 67, 3: 5}Output:{1:100, 2:90, 3:5, 5:67, 8:3}As shown above, we can see the dictionary is sorted according to its keys.Example Live Demodic={2:90, ... Read More

Rear Element Extraction from List of Tuples in Python

AmitDiwan
Updated on 11-Mar-2021 09:17:06

1K+ Views

If it is required to extract the rear element from a list of tuples, it can be done using list comprehension and negative indexing.The list comprehension is a shorthand to iterate through the list and perform operations on 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).A list of tuple basically contains tuples enclosed in a list.Below is a demonstration of the same −ExampleLive Demomy_list = [('Will', 67, 45), ('Jam', 34, 56), ('Pow', 99, 123), ('Nyk', 0, 56)] print ("The list of tuples is : ... Read More

Bubble Sort in Python: Explanation and Example

pawandeep
Updated on 11-Mar-2021 09:14:09

2K+ Views

Bubble sort is a sorting algorithm to sort a list into ascending (or descending) order. This is the easiest sorting algorithm but it is not very efficient. It can be used on small input sizes but not time efficient for lists or arrays with larger length. Its time complexity is O(n^2). However, this is an in-place sorting algorithm, which means it doesn’t use any extra space. Thus, its efficient in terms of space complexity. However, it is not used much since there are better sorting algorithms than bubble sort.How does Bubble Sort work?In bubble sort, two for loops are used. ... Read More

Explain Binary Search in Python

pawandeep
Updated on 11-Mar-2021 09:11:49

4K+ Views

Binary search is a searching algorithm which is used to search an element from a sorted array. It cannot be used to search from an unsorted array. Binary search is an efficient algorithm and is better than linear search in terms of time complexity.The time complexity of linear search is O(n). Whereas the time complexity of binary search is O(log n). Hence, binary search is efficient and faster-searching algorithm but can be used only for searching from a sorted array.How does Binary Search work?The basic idea behind binary search is that instead of comparing the required element with all the ... Read More

Index of Minimum Value Record in Python

AmitDiwan
Updated on 11-Mar-2021 09:11:08

233 Views

If it is required to find the tuple corresponding to the minimum value of other tuple index, it can be done using the 'min' method, and the 'operator.itemgetter' method.The 'min' method gives the minimum of the elements in the iterable. The itemgetter fetches a specific item from its operand.Below is a demonstration of the same −ExampleLive Demofrom operator import itemgetter my_list = [('Will', 45), ('Jam', 13), ('Pow', 89), ('Nyk', 56)] print ("The list is: " ) print(my_list) my_result = min(my_list, key = itemgetter(1))[0] print ("The value with minimum score is : " ) print(my_result)OutputThe list is: [('Will', ... Read More

Read CSV File in Python

pawandeep
Updated on 11-Mar-2021 09:10:51

4K+ Views

A CSV file stands for Comma Separated Values file. It is a plain text file in which data values are separated by commas and hence represent a tabular data in the form of plain text with the help of commas. A CSV file has .csv extension.Here’s how a CSV files look like −Sr.No, Name, City, Age 1, Rahul, Kolkata, 21 2, Karan, Amritsar, 23 3, Priya, Bangalore, 20To create a CSV file, you can simply write the file in above format in notepad and save it with .csv extension.Read CSV file in PythonThe csv file stored on your local storage ... Read More

Cumulative Nested Tuple Column Product in Python

AmitDiwan
Updated on 11-Mar-2021 09:10:18

228 Views

If it is required to find the cumulative column product of a nested tuple, the 'zip' method and a nested generator expression can be used.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 zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −ExampleLive Demotuple_1 = ((11, 23), (41, 25), (22, 19)) tuple_2 = ((60, 73), (31, 91), ... Read More

Maximum Value in Record List as Tuple Attribute in Python

AmitDiwan
Updated on 11-Mar-2021 09:09:57

246 Views

When it is required to find the maximum value in a record list of tuples, the list comprehension and the 'max' methods can be used.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 list comprehension is a shorthand to iterate through the list and perform operations on it.The 'max' method can be used to find the maximum of all the elements in an iterable.Below is a demonstration of the same −ExampleLive Demomy_list = [('Will', [67, ... Read More

Sort a List of Tuples Alphabetically in Python

AmitDiwan
Updated on 11-Mar-2021 09:09:39

2K+ Views

When it is required to sort a list of tuples in alphabetical order, the 'sort' method can be used. When using this, the contents of the original tuple get changed, since in-place sorting is performed.The 'sort' function sorts the values in ascending order by default. If the order of sorting is specified as descending, it is sorted in descending order.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 ... Read More

Advertisements