AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 134 of 840

Python program to insert a new node at the beginning of the Circular Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 677 Views

When it is required to insert a new node at the beginning of a circular linked list, a 'Node' class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node. Instead, the last node points back to the first node. Node Class Structure First, we create ...

Read More

Python program to find the maximum and minimum value node from a circular linked list

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 395 Views

When it is required to find the maximum and minimum node values from a circular linked list, a Node class needs to be created. In this class, there are two attributes: the data present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the tail are adjacent to each other. They are connected to form a circle, and don't have None value in the last node − instead, the last node points back to the first node. Implementation The implementation involves creating a Node ...

Read More

Python program to delete a node from the middle of the Circular Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 281 Views

When it is required to delete a node from the middle of the circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have NULL value in the last node. Another class needs to be created that would have an initialization function, and the head of the node ...

Read More

Python program to delete a node from the end of the Circular Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 435 Views

When it is required to delete a node from the end of a circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the tail are adjacent to each other. They are connected to form a circle, and don't have NULL value in the last node − instead, the last node points back to the first node. Node Class Structure The Node class ...

Read More

Python program to create and display a Circular Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 857 Views

A circular linked list is a data structure where the last node points back to the first node, forming a circle. Unlike regular linked lists that end with None, circular linked lists have no end − the traversal can continue indefinitely. To implement a circular linked list in Python, we need two classes: a Node class to represent individual elements, and a CircularLinkedList class to manage the list operations. Node Class Structure Each node contains data and a reference to the next node ? class Node: def __init__(self, data): ...

Read More

Python program to create a Circular Linked List of n nodes and display it in reverse order

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 244 Views

When it is required to create a circular linked list and display it in the reverse order, a Node class needs to be created. However, the example below demonstrates a regular linked list implementation, not a true circular linked list. In a circular linked list, the last node points back to the first node instead of pointing to None, forming a circle. The head and tail are connected to each other. To display the data elements in reverse order, we can either reverse the list structure or traverse it backwards. Regular Linked List (Current Implementation) The current ...

Read More

Python Program to Shuffle Deck of Cards

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

When it is required to shuffle a deck of cards using Python, the itertools and the random packages need to be used. Random library has a method named shuffle() that can be used to mix up and randomize the data. Creating and Shuffling a Deck Below is a demonstration of how to create a standard deck and shuffle it ? import itertools import random # Create a deck of cards using itertools.product deck = list(itertools.product(range(1, 14), ['Spade', 'Heart', 'Diamond', 'Club'])) print("Original deck created with", len(deck), "cards") print("The cards are being shuffled...") # Shuffle ...

Read More

Sort list of tuples by specific ordering in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 415 Views

When you need to sort a list of tuples by a specific element or custom ordering, Python's sorted() function with a key parameter provides a flexible solution. The key parameter accepts a function that determines the sorting criteria. A list of tuples is a common data structure where each tuple contains related elements. Sorting such lists by specific tuple elements is frequently needed in data processing tasks. Sorting by Second Element Use a lambda function to specify which tuple element to sort by ‒ def tuple_sort(my_tup): return sorted(my_tup, key=lambda x: x[1]) ...

Read More

Remove tuple from list of tuples if not containing any character in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 573 Views

When working with lists of tuples, you may need to remove tuples that don't contain any alphabetic characters. This can be achieved using list comprehension combined with the any() function and isalpha() method. A list of tuples contains tuples enclosed in a list, where each tuple can hold heterogeneous data types. List comprehension provides a concise way to filter and transform data based on specific conditions. Syntax filtered_list = [(a, b) for a, b in original_list if any(c.isalpha() for c in a)] Example Let's remove tuples whose first element doesn't contain any alphabetic ...

Read More

Remove tuples from list of tuples if greater than n in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 485 Views

When working with lists of tuples, you may need to remove tuples based on a numeric condition. This can be accomplished using list comprehension, the filter() function, or lambda expressions to check if tuple values are greater than a threshold n. A lambda function is an anonymous function defined without a name using the lambda keyword. It takes any number of arguments but contains only a single expression, making it perfect for simple filtering operations. Using List Comprehension The most Pythonic way is using list comprehension to filter tuples ? my_tuples = [('a', 130), ('b', ...

Read More
Showing 1331–1340 of 8,392 articles
« Prev 1 132 133 134 135 136 840 Next »
Advertisements