Python Articles

Page 43 of 855

Python Program to Add Corresponding Positioned Elements of 2 Linked Lists

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 212 Views

When it is required to add the corresponding elements of specific position in two linked lists, a method to add elements to the linked list, a method to print the elements of the linked list, and a method to add elements to corresponding positions of a linked list are defined. Two lists instances are created and the previously defined method is called on these linked list instances.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class LinkedList_structure:    def __init__(self):       ...

Read More

Python program to count Bidirectional Tuple Pairs

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 254 Views

When it is required to count the number of bidirectional tuple pairs in a list of tuples, the list can be iterated over using nested loops, and the ‘AND’ operation is performed on the first element and result of equality between first and second element.Below is a demonstration of the same −Examplemy_list = [(45, 67), (11, 23), (67, 45), (23, 11), (0, 9), (67, 45)] print("The list is : ") print(my_list) my_result = 0 for idx in range(0, len(my_list)):    for iidx in range(idx + 1, len(my_list)):       if my_list[iidx][0] == my_list[idx][1] and my_list[idx][1] == my_list[iidx][0]: ...

Read More

Python Program to Find the first Common Element between the 2 given Linked Lists

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 337 Views

When it is required to find the common element that occurs for the first time between two linked lists, a method to add elements to the linked list, and a method to get the common element that occurs for the first time in these linked lists is defined.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class LinkedList_structure:    def __init__(self):       self.head = None       self.last_node = None    def add_vals(self, data):       if self.last_node ...

Read More

Python program to unique keys count for Value in Tuple List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 196 Views

When it is required to get the count of unique keys for values in a list of tuple, it can be iterated over and the respective counts can be determined.Below is a demonstration of the same −Exampleimport collections my_result = collections.defaultdict(int) my_list = [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]] print("The list of list is :") print(my_list) for elem in my_list:    my_result[elem[0]] += 1 print("The result is : ") print(my_result)OutputThe list of list is : [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]] The result is : defaultdict(, {('Hello', 'Hey'): 2, ('Jane', 'Will'): ...

Read More

Python Program to Find Number of Occurrences of All Elements in a Linked List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 214 Views

When it is required to find the number of occurences of all the elements of a linked list, a method to add elements to the linked list, a method to print the elements and a method to find the occurrence of all elements in the linked list are defined.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class LinkedList_structure:    def __init__(self):       self.head = None       self.last_node = None    def add_vals(self, data):       if ...

Read More

Python Program to Reverse only First N Elements of a Linked List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 275 Views

When it is required to reverse a specific set of elements in a linked list, a method named ‘reverse_list’ is defined. This iterates through the list, and reverses the specific set of elements.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class LinkedList_structure:    def __init__(self):       self.head = None       self.last_node = None    def add_vals(self, data):       if self.last_node is None:          self.head = Node(data)          self.last_node = ...

Read More

Python Program to Select the nth Smallest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 521 Views

When it is required to select the nth smallest element from a list in linear time complexity, two methods are required. One method to find the smallest element, and another method that divides the list into two parts. This division depends on the ‘i’ value that is given by user. Based on this value, the list is split, and the smallest element is determined.Below is a demonstration of the same −Exampledef select_smallest(my_list, beg, end, i):    if end - beg k:       return select_smallest(my_list, pivot_val + 1, end, i - k)    return my_list[pivot_val] def ...

Read More

Python Program to Select the nth Largest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 452 Views

When it is required to select the nth largest element from a list in linear time complexity, two methods are required. One method to find the largest element, and another method that divides the list into two parts. This division depends on the ‘i’ value that is given by user. Based on this value, the list is split, and the largest element is determined.Below is a demonstration of the same −Exampledef select_largest(my_list, beg, end, i):    if end - beg k:       return select_largest(my_list, beg, pivot_val, i - k)    return my_list[pivot_val] def start_partition(my_list, beg, ...

Read More

Python Program to Reverse a Stack using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 708 Views

When it is required to reverse a stack data structure using recursion, a ‘stack_reverse’ method, in addition to methods to add value, delete value, and print the elements of the stack are defined.Below is a demonstration of the same −Exampleclass Stack_structure:    def __init__(self):       self.items = []    def check_empty(self):       return self.items == []    def push_val(self, data):       self.items.append(data)    def pop_val(self):       return self.items.pop()    def print_it(self):       for data in reversed(self.items):          print(data) def insert_bottom(instance, data):   ...

Read More

Python Program to Implement Stack Using Two Queues

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 617 Views

When it is required to implement a stack using two queues, a ‘Stack_structure’ class is required along with a Queue_structure class. Respective methods are defined in these classes to add and delete values from the stack and queue respectively.Below is a demonstration of the same −Exampleclass Stack_structure:    def __init__(self):       self.queue_1 = Queue_structure()       self.queue_2 = Queue_structure()    def check_empty(self):       return self.queue_2.check_empty()    def push_val(self, data):       self.queue_1.enqueue_operation(data)       while not self.queue_2.check_empty():          x = self.queue_2.dequeue_operation()          self.queue_1.enqueue_operation(x)     ...

Read More
Showing 421–430 of 8,547 articles
« Prev 1 41 42 43 44 45 855 Next »
Advertisements