Programming Articles

Page 416 of 2547

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 280 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 Print an Inverted Star Pattern

Akshitha Mote
Akshitha Mote
Updated on 25-Mar-2026 1K+ Views

In this article, we'll learn how to print inverted star patterns in Python. An inverted star pattern displays stars in descending order, where each row has fewer stars than the previous one. For example, with N=5 rows ? ***** **** *** ** * Basic Inverted Star Pattern The most common approach uses nested for loops − the outer loop controls rows, and the inner loop prints stars for each row. Using Nested Loops Here's how to create an inverted star pattern with spaces between stars ? # Number of rows N ...

Read More

Python program to create and display a Circular Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 853 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 242 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 408 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 566 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 482 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

Update a list of tuples using another list in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 376 Views

When you need to update a list of tuples using another list, the defaultdict from the collections module provides an elegant solution. This approach allows you to merge tuples with matching keys and aggregate their values. defaultdict is a container similar to dictionaries that automatically creates missing keys with a default value. It's a subclass of the dict class that never raises a KeyError, making it perfect for grouping operations. Example Here's how to merge two lists of tuples and keep the maximum value for each key ? from collections import defaultdict def merge_vals(list_1, ...

Read More
Showing 4151–4160 of 25,466 articles
« Prev 1 414 415 416 417 418 2547 Next »
Advertisements