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

AmitDiwan
Updated on 25-Mar-2026 17:45:28

252 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
Updated on 25-Mar-2026 17:45:06

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
Updated on 25-Mar-2026 17:44:45

422 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
Updated on 25-Mar-2026 17:44:22

583 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
Updated on 25-Mar-2026 17:44:05

495 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
Updated on 25-Mar-2026 17:43:45

389 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

Selective value selection in list of tuples in Python

AmitDiwan
Updated on 25-Mar-2026 17:43:26

206 Views

When you need to extract specific values from a list of tuples based on certain keys, Python provides an elegant solution using the dict() method combined with list comprehension and the get() method. A list of tuples contains tuples enclosed in a list, often representing key-value pairs. The dict() method can convert this structure into a dictionary for efficient lookups. The get() method safely retrieves values from a dictionary, returning a default value if the key doesn't exist. Basic Example Here's how to select specific values from a list of tuples ? # List of ... Read More

Split tuple into groups of n in Python

AmitDiwan
Updated on 25-Mar-2026 17:43:06

673 Views

When it is required to split a tuple into groups of 'n' elements, list comprehension can be used along with slicing. A tuple is an immutable data type, meaning values once defined cannot be changed by accessing their index elements. If we try to change the elements, it results in an error. Tuples are important containers since they ensure read-only access. List comprehension is a shorthand to iterate through sequences and perform operations on them efficiently. Method 1: Using List Comprehension with Slicing The most common approach uses list comprehension with range() and slicing ? ... Read More

Modifying tuple contents with list in Python

AmitDiwan
Updated on 25-Mar-2026 17:42:46

313 Views

When it is required to modify the contents of tuples within a list, the zip() method combined with list comprehension provides an efficient solution. The zip() method takes iterables, aggregates them element-wise into tuples, and returns an iterator. List comprehension provides a concise way to iterate through lists and perform operations on them. A list can store heterogeneous values (data of any type like integers, strings, floats, etc.). When working with a list of tuples, you often need to modify specific elements within each tuple using values from another list. Example Here's how to modify the ... Read More

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

AmitDiwan
Updated on 25-Mar-2026 17:42:29

712 Views

When removing tuples with duplicate first values from a list of tuples, you can use a set to track visited first elements. This approach preserves the first occurrence of each unique first value. Using Set to Track First Values The most efficient approach uses a set to remember which first values have been seen ? tuples_list = [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), ... Read More

Advertisements