Python Articles

Page 11 of 854

Extract tuples having K digit elements in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 519 Views

When it is required to extract tuples that have a specific number of elements, list comprehension can be used. It iterates over the elements of the list of tuple and puts forth condition that needs to be fulfilled. This will filter out the specific elements and stores them in another variable.Below is a demonstration of the same −Examplemy_list = [(34, 56), (45, 6), (111, 90), (11, 35), (78, )] print("The list is : ") print(my_list) K = 2 print("The value of K has been initialized to" + "str(K)") my_result = [sub for sub in my_list if all(len(str(elem)) ...

Read More

Convert key-values list to flat dictionary in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 661 Views

When it is required to convert a dictionary, that contains pairs of key values into a flat list, dictionary comprehension can be used.It iterates through the dictionary and zips them using the ‘zip’ method.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −Examplefrom itertools import product my_dict = {'month_num' : [1, 2, 3, 4, 5, 6], 'name_of_month' : ['Jan', 'Feb', 'March', 'Apr', 'May', 'June']} print("The dictionary is : ") print(my_dict) my_result = dict(zip(my_dict['month_num'], my_dict['name_of_month'])) print("The flattened dictionary is: ") print(my_result)OutputThe dictionary is : ...

Read More

Append Dictionary Keys and Values (In order ) in dictionary using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 852 Views

When it is required to append the keys and values of a dictionary in order, the ‘list’ method can be used. Along with this, the ‘.keys’ and ‘.values’ method can be used access the specific keys and values of the dictionary.Below is a demonstration of the same −Examplemy_dict = {"January" : 1, "Feb" : 2, "March" : 3, 'April':4, 'May' : 5, 'June' :6} print("The dictionary is : ") print(my_dict) my_result = list(my_dict.keys()) + list(my_dict.values()) print("The ordered key and value are : ") print(my_result)OutputThe dictionary is : {'January': 1, 'Feb': 2, 'March': 3, 'April': 4, 'May': 5, ...

Read More

Python Program to Create a Linked List & Display the Elements in the List

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

When it is required to create a linked list, and display the elements of this linked list, a method to add values to the linked list, as well as a method to display the elements of a Linked List.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None       self.last_node = None    def add_value(self, my_data):       if self.last_node is None:          self.head = Node(my_data) ...

Read More

Python Program to Display the Nodes of a Linked List in Reverse without using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 246 Views

When it is required to display the nodes of a linked list in reverse without using the method of recursion, a method to add elements to the linked list, and a method to display the elements in reverse order is defined.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None       self.last_node = None    def add_value(self, my_data):       if self.last_node is None:          self.head ...

Read More

Python Program to Search for an Element in the Linked List without using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 554 Views

When it is required to search for an element in a linked list without using recursion method, a method to add values to the linked list, as well as a method to display the elements of a Linked List.It would also have a method that helps find the index of the element that is being searched.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None       self.last_node = None   ...

Read More

Python Program to Find the Length of the Linked List without using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 246 Views

When it is required to find the length of a linked list without using recursion, a method to add elements to the linked list, and a method to calculate the length of the linked list is defined.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None       self.last_node = None    def add_value(self, my_data):       if self.last_node is None:          self.head = Node(my_data)     ...

Read More

Python Program to Display all the Nodes in a Linked List using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 650 Views

When it is required to display the elements/nodes in a linked list, using recursion method, a method to add values to the linked list, and a method to print the elements of a Linked List. It would also have a helper method that uses recursion, i.e calls the helper function again and again to print the values.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None       self.last_node = None ...

Read More

Python Program to Check String is Palindrome using Stack

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 973 Views

When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not.Below is a demonstration for 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() my_instance = Stack_structure() text_input = input('Enter ...

Read More

Python Program to Print the Alternate Nodes in a Linked List using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 201 Views

When it is required to print the alternate nodes in a linked list using recursion, a method to add elements to the linked list, a method to display the elements of the linked list, and a method to get the alternate values of a linked list are defined. Another helper function is used that calls the previously defined method to get alternate values.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, data):       self.data = data       self.next = None class my_linked_list:    def __init__(self):       self.head = None   ...

Read More
Showing 101–110 of 8,532 articles
« Prev 1 9 10 11 12 13 854 Next »
Advertisements