Programming Articles

Page 421 of 2547

Python Program to Implement Shell Sort

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

Shell Sort is an efficient sorting algorithm that improves upon insertion sort by comparing elements separated by a gap (interval). It starts with a large gap and gradually reduces it until the gap becomes 1, at which point it performs a final insertion sort on the nearly sorted array. The algorithm works by sorting sub-arrays of elements that are a certain distance apart, making the overall array more organized with each pass ? How Shell Sort Works Shell Sort follows these steps ? Start with a gap (interval) equal to half the array length Compare ...

Read More

Sort lists in tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 416 Views

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 without modifying the original. Generator expression 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 ...

Read More

Custom sorting in list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 867 Views

Custom sorting in Python allows you to sort a list of tuples based on specific criteria using the sort() method with a key function. This is useful when you need to sort by a particular element within each tuple. Basic Custom Sorting The sort() method sorts elements in-place. Use the key parameter to specify which element of the tuple to sort by ? def tuple_sort(my_tup): my_tup.sort(key=lambda x: x[1]) return my_tup my_tuple = [('Will', 100), ('John', 67), ('Harold', 86), ('Jane', 35)] print("Original tuple:") print(my_tuple) print("Sorted by second ...

Read More

Get tuple element data types in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 915 Views

When you need to determine the data types of elements in a tuple, you can use Python's map() function combined with the type() function. The map() function applies a given function to every item in an iterable (such as list, tuple) and returns an iterator. The type() function returns the class type of any object passed to it. Basic Example Here's how to get data types of tuple elements ? my_tuple = ('Hi', 23, ['there', 'Will']) print("The tuple is:") print(my_tuple) # Apply type() to each element using map() data_types = list(map(type, my_tuple)) ...

Read More

Check order specific data type in tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 515 Views

When working with tuples containing mixed data types, you might need to verify that elements appear in a specific order with expected data types. Python's isinstance() method combined with chained conditions provides an effective way to validate tuple structure. The isinstance() method checks if a given object belongs to a specific data type. Chained conditionals use the and operator to combine multiple conditions, ensuring all must be True for the overall result to be True. Basic Example Let's check if a tuple follows the pattern: string, list, integer ? my_tuple = ('Hi', ['there', 'Will'], 67) ...

Read More

Convert location coordinates to tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 560 Views

When working with location data, you often need to convert coordinate strings into tuple format for mathematical operations or data processing. Python provides several methods to accomplish this conversion safely and efficiently. Method 1: Using eval() (Not Recommended) The eval() method can parse and execute string expressions, but it poses security risks ? my_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) print("Type:", type(my_result)) The string is: 67.5378, -78.8523 The coordinates after converting the string to tuple is: ...

Read More

Intersection in Tuple Records Data in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 678 Views

When it is required to find the intersection of data in tuple records, a list comprehension can be used to identify common elements between two lists of tuples. The list comprehension is a shorthand to iterate through the list and perform operations on it. It provides an efficient way to compare tuple elements across multiple lists. 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 tuples basically contains tuples enclosed in a list. Example Here's how to find the ...

Read More

Check if tuple and list are identical in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 401 Views

When checking if a tuple and list are identical in Python, we need to compare their elements at corresponding positions. Python provides several approaches: using a loop, the all() function with zip, or direct comparison after type conversion. Using a Simple Loop This method iterates through both collections and compares elements at each index ? my_tuple = ('Hi', 'there', 'Will') my_list = ['Hi', 'there', 'Will'] print("The tuple is:") print(my_tuple) print("The list is:") print(my_list) # Check if lengths are equal first if len(my_tuple) != len(my_list): result = False else: ...

Read More

Common words among tuple strings in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 378 Views

When it is required to find common words among tuple strings, the join() method, the set() method, the & operator and the split() method can be used together. The join() method can be used to join multiple values based on a specific delimiter. 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. The split() function splits the given data into multiple sections depending on the delimiter. The & operator performs intersection (AND operation) between sets. ...

Read More

Conversion to N*N tuple matrix in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 485 Views

When working with tuples in Python, you might need to convert an N*N tuple structure into a matrix format by padding shorter tuples with zeros. This can be accomplished using a simple loop and the * operator to repeat values. The * operator in Python can multiply values and also repeat elements. For example, (0, ) * 3 creates (0, 0, 0). Converting Tuple to N*N Matrix Here's how to pad tuples with zeros to create a uniform matrix structure ? my_tuple_1 = ((11, 14), (0, 78), (33, 11), (10, 78)) print("The tuple of ...

Read More
Showing 4201–4210 of 25,466 articles
« Prev 1 419 420 421 422 423 2547 Next »
Advertisements