Found 26504 Articles for Server Side Programming

Update a list of tuples using another list in Python

AmitDiwan
Updated on 13-Mar-2021 05:43:19

316 Views

When it is required to update a list of tuple using another list, the 'defaultdict' can be used.Defaultdict is a container similar to dictionaries which is present in 'collections' module. It is a sub-class of the 'dict' class. It returns a dictionary-like object. The 'defaultdict' doesn't raise a KeyError ever. It provides a default value for the key which doesn't exist.Below is a demonstration for the same −ExampleLive Demofrom collections import defaultdict def merge_vals(list_1, list_2):    my_dict = defaultdict(list)    for i, j in list_1 + list_2:       my_dict[i].append(j)        return sorted([(i, max(j)) for i, j ... Read More

Selective value selection in list of tuples in Python

AmitDiwan
Updated on 13-Mar-2021 05:42:47

144 Views

When it is required to find the selective value in a list of tuples, the 'dict' method, the 'get' method and the list comprehension can be used.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.The 'dict' method creates a dictionary. It contains key value pairs, which are unordered, and indexed.The 'get' method returns the value of a specific key when ... Read More

Split tuple into groups of n in Python

AmitDiwan
Updated on 13-Mar-2021 05:39:36

566 Views

When it is required to split the tuple into 'n' groups, the list comprehension can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.The list comprehension is a shorthand to iterate through the list and perform operations on it.Below is a demonstration for the same −ExampleLive Demomy_tuple = (12, 34, 32, 41, 56, 78, 9, 0, 87, 53, 12, 45, 12, 6) print ("The tuple ... Read More

Modifying tuple contents with list in Python

AmitDiwan
Updated on 13-Mar-2021 05:39:00

231 Views

When it is required to modify the list of tuple, the 'zip' method and the list comprehension can be used.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.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 for the same −ExampleLive Demomy_list_1 = [('Hi', 1), ('there', 2), ('Jane', 3)] my_list_2 = ... Read More

Remove tuples having duplicate first value from given list of tuples in Python

AmitDiwan
Updated on 13-Mar-2021 05:38:17

630 Views

When it is required to remove tuples that have a duplicate first value from a given set of list of tuples, a simple 'for' loop, and the 'add' and 'append' methods can be used.Below is a demonstration for the same −ExampleLive Demomy_input = [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), (45.324, 'You are the best.')] visited_data = set() my_output_list = [] for a, b in my_input:    if not a in visited_data:       visited_data.add(a)       my_output_list.append((a, b)) print("The list of tuple is : ") print(my_input) print("The list of tuple after ... Read More

Group Use declarations in PHP 7

Urmila Samariya
Updated on 13-Mar-2021 06:12:31

917 Views

In PHP 7, Group Use declaration is more readable and can be used to import classes, constants, and functions easily from the same namespace.Group Use declaration is used to import multiple structures easily from a namespace and cuts a good level of volubility in most cases. It is also useful to identify the multiple imported entities which belong to the same module.Example 1The following example shows the code before PHP 7 −Example 2The following example shows the code for PHP7 or PHP 7+use com\India\{ClassX, ClassY, ClassZ as Z}; use function com\India\{fn_x, fn_y, fn_z}; use const com\India\{ConstX, ConstY, ConstZ};ExplanationIn Example 1, ... Read More

Unpacking tuple of lists in Python

AmitDiwan
Updated on 13-Mar-2021 05:37:03

381 Views

When it is required to unpack a tuple of list, the 'reduce' method can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.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 tuple of list contains multiple lists, which are enclosed in '(' and ')'.The 'reduce' method is used to apply a specific ... Read More

Python Program to Implement Binary Search with Recursion

Farhan Muhamed
Updated on 20-Aug-2025 17:36:53

4K+ Views

The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration. Binary Search Using Recursion The following are the steps to implement binary search using recursion: Define a binSearch() function that accepts the array, the left index, the right index, and the target that you want to search. Check if the ... Read More

Python Program to Implement Binary Search without Recursion

SaiKrishna Tavva
Updated on 12-Dec-2024 17:32:55

5K+ Views

To implement binary search without recursion, the program takes a sorted list and a key as input. It uses the Binary search method, which is an efficient way to find a specific item in a sorted list. It works by repeatedly dividing the list in half. This method works by first comparing the key with the middle element of the list. If the key matches the middle element, the search is successful, and the index is returned. If the key is less than the middle element, the search continues on the left subarray; if it's greater, the search shifts to ... Read More

Remove matching tuples in Python

AmitDiwan
Updated on 13-Mar-2021 05:35:47

294 Views

When it is required to remove the matching tuples from two list of tuples, the list comprehension 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.Below is a demonstration for the same −ExampleLive Demomy_list_1 = [('Hi', 'there'), ('Jane', 'Hi'), ('how', 'are'), ('you', '!')] my_list_2 = [('Hi', 'there'), ('Hi', 'Jane')] print("The first list is : ") print(my_list_1) print("The second ... Read More

Advertisements