Python Articles

Page 23 of 854

Remove Tuples from the List having every element as None in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 438 Views

When it is required to remove tuples from a list of tuples where a ‘None’ element is present, a list comprehension can be used.Below is a demonstration of the same −Examplemy_list = [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), (None, 45, 6)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if not all(elem == None for elem in sub)] print("The None tuples have been removed, the result is : " ) print(my_result)OutputThe list is : [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), ...

Read More

Consecutive Nth column Difference in Tuple List using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 232 Views

When it is required to find the consecutive column difference in a list of tuple, it can be iterated over, and the ‘abs’ method and the ‘append’ method can be used.The ‘abs’ method returns the absolute or positive value, and the append adds elements to a list.Below is a demonstration of the same −Examplemy_list = [(67, 89, 32), (11, 23, 44), (65, 75, 88)] print("The list is : ") print(my_list) print("The value of k has been initialized") K = 1 my_result = [] for idx in range(0, len(my_list) - 1):    my_result.append(abs(my_list[idx][K] - my_list[idx + 1][K])) ...

Read More

Python program to Sort Tuples by their Maximum element

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 355 Views

When it is required to sort the tuples based on the maximum element in it, a method is defined that uses the ‘max’ method to return the highest element.Next the ‘sort’ method can be used to sort the list based on the previously defined function.Below is a demonstration of the same −Exampledef get_max_value(my_val):    return max(my_val) my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] print(“The list is : “) print(my_list) my_list.sort(key = get_max_value, reverse = True) print(“The sorted tuples are : “) print(my_list)OutputThe list is : [(4, 6, 8, ...

Read More

Python program to find tuples which have all elements divisible by K from a list of tuples

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 954 Views

When it is required to find tuples that have elements that are divisible by a specific element ‘K’, the list comprehension can be used.Below is a demonstration of the same −Examplemy_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print("The list is : ") print(my_list) K = 45 print("The value of K has been initialized to ") print(K) my_result = [sub for sub in my_list if all(ele % K == 0 for ele in sub)] print("Elements divisible by K are : " + str(my_result))OutputThe list is : [(45, 90, 135), (71, 92, 26), (2, 67, ...

Read More

Python program to find Tuples with positive elements in List of tuples

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 647 Views

When it is required to find tuples that have position elements from a list of tuples, list comprehension can be used.Below is a demonstration of the same −Examplemy_list = [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if all(elem >= 0 for elem in sub)] print("The positive elements are : ") print(my_result)OutputThe list is : [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] The positive elements are : [(56, 43), (24, 56)]ExplanationA list of tuples is defined, and is displayed ...

Read More

Python Program to Convert a given Singly Linked List to a Circular List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 418 Views

When it is required to convert a singly linked list into a circular linked list, a method named ‘convert_to_circular_list’ is defined that ensures that the last elements points to the first element, thereby making it circular in nature.Below is a demonstration of the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class LinkedList_struct:    def __init__(self):       self.head = None       self.last_node = None    def add_elements(self, data):       if self.last_node is None:          self.head = Node(data)   ...

Read More

Python Program to Implement a Stack

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 4K+ Views

When it is required to implement a stack using Python, a stack class is created, and an instance of this class is created. Methods to push, pop elements are defined and the instance is used to call these methods.Below is a demonstration of the same −Exampleclass Stack_struct:    def __init__(self):       self.items = []    def check_empty(self):       return self.items == []    def add_elements(self, my_data):       self.items.append(my_data)    def delete_elements(self):       return self.items.pop() my_instance = Stack_struct() while True:    print('Push ')    print('Pop')    print('Quit')    my_input ...

Read More

Python Program to Find Nth Node in the Inorder Traversal of a Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 280 Views

When it is required to find the ‘n’th node using inorder traversal of a binary tree, a binary tree class is created with methods to set the root element, add elements to left or right, perform in order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is a demonstration of the same −Exampleclass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key ...

Read More

Check whether a number has consecutive 0's in the given base or not using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 255 Views

When it is required to check if a number has consecutive zeroes of a specific base, a method is defined, that takes the number and base as parameters, and uses another method to return Yes or No depending on whether the base is present or not.Below is a demonstration of the same −Exampledef check_consecutive_zero(N, K):    my_result = convert_to_base(N, K)    if (check_n(my_result)):       print("Yes")    else:       print("No") def convert_to_base(N, K):    weight = 1    s = 0    while (N != 0):       r = N % K     ...

Read More

Python Program to Construct a Tree & Perform Insertion, Deletion, Display

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 702 Views

When it is required to construct a binary tree, and perform operations such as inserting an element, deleting an element and displaying elements of the tree, a class is defined with methods in it. An instance of the class is defined and this is used to access the elements and perform operations.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None, parent=None):       self.key = data       self.children = []       self.parent = parent    def set_root(self, data):       self.key = data    def add_node(self, node):   ...

Read More
Showing 221–230 of 8,533 articles
« Prev 1 21 22 23 24 25 854 Next »
Advertisements