Found 10476 Articles for Python

Python program to create and display a Circular Linked List

AmitDiwan
Updated on 13-Mar-2021 05:53:53

792 Views

When it is required to create a circular linked list and display it, 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 'linked_list' class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'. Below ... Read More

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

AmitDiwan
Updated on 13-Mar-2021 05:51:55

187 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.To display the data elements in the circular list in reverse order, another method can be defined, that would reverse the data. 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 ... Read More

Python program to create a Circular Linked List of N nodes and count the number of nodes

AmitDiwan
Updated on 13-Mar-2021 05:51:06

192 Views

When it is required to create a circular linked list that has 'N' nodes, and get the count of the number of nodes, a 'Node' class needs to be created. To display the data elements in the circular list, another method can be defined, that would display the data. 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' ... Read More

Python Program to Shuffle Deck of Cards

AmitDiwan
Updated on 13-Mar-2021 05:50:44

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 show the data.Below is a demonstration for the same −ExampleLive Demoimport itertools, random my_deck = list(itertools.product(range(1, 11), ['Spade', 'Heart', 'Diamond', 'Club'])) print("The cards are being shuffled") random.shuffle(my_deck) print("Cards are drawn at random") print("They are : ") for i in range(5):    print(my_deck[i][0], "of", my_deck[i][1])OutputThe cards are being shuffled Cards are drawn at random They are : 1 of Diamond 5 of Diamond 4 ... Read More

Python Circular Linked List Program

AmitDiwan
Updated on 13-Mar-2021 05:47:46

9K+ Views

When it is required to create a Python program that generates a linked list, a 'Node' class needs to be created. To display the data elements in the circular list, another method can be defined, that would display the data. 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 'linked_list' class needs ... Read More

Sort list of tuples by specific ordering in Python

AmitDiwan
Updated on 13-Mar-2021 05:45:54

352 Views

When it is required to sort the list of tuples in a specific order, the 'sorted' method can be used.The 'sorted' method is used to sort the elements of a list.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list.Below is a demonstration for the same −ExampleLive Demodef tuple_sort(my_tup):    return(sorted(my_tup, key = lambda x: x[1])) my_tuple = [('Mahe', 11), ('Aisha', 33), ('Will', 50), ('Root', 65)] print("The list of tuple is : ") print(my_tuple) ... Read More

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

AmitDiwan
Updated on 13-Mar-2021 05:45:21

496 Views

When it is required to remove a tuple from a list of tuples based on a given condition, i.e the tuple doesn't contain a specific character, the list comprehension can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list.The list comprehension is a shorthand to iterate through the list and perform operations on it.Below is a demonstration for the same −ExampleLive Demomy_list = [('. ', 62), ('Mark', 5),    ('Paul.', 21), ('.....', 0),   ... Read More

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

AmitDiwan
Updated on 13-Mar-2021 05:43:43

431 Views

When it is required to remove tuples from a list of tuples if it is greater than a value 'n', the lambda function can be used.Anonymous function is a function which is defined without a name. In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword.It takes a single expression, but can take any number of arguments. It uses the expression and returns the result of it.Below is a demonstration for the same −ExampleLive Demomy_tuple = [('a', 130), ('b', 230), ('c', 25), ('z', 654), ('f', 69)] print("The list ... Read More

Update a list of tuples using another list in Python

AmitDiwan
Updated on 13-Mar-2021 05:43:19

317 Views

When it is required to update a list of tuple using another list, the 'defaultdict' can be used.Defaultdict is a container similar to dictionaries which is present in 'collections' module. It is a sub-class of the 'dict' class. It returns a dictionary-like object. The 'defaultdict' doesn't raise a KeyError ever. It provides a default value for the key which doesn't exist.Below is a demonstration for the same −ExampleLive Demofrom collections import defaultdict def merge_vals(list_1, list_2):    my_dict = defaultdict(list)    for i, j in list_1 + list_2:       my_dict[i].append(j)        return sorted([(i, max(j)) for i, j ... Read More

Selective value selection in list of tuples in Python

AmitDiwan
Updated on 13-Mar-2021 05:42:47

146 Views

When it is required to find the selective value in a list of tuples, the 'dict' method, the 'get' method and the list comprehension can be used.The list comprehension is a shorthand to iterate through the list and perform operations on it. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The 'dict' method creates a dictionary. It contains key value pairs, which are unordered, and indexed.The 'get' method returns the value of a specific key when ... Read More

Advertisements