Python Articles

Page 74 of 855

Sort tuple based on occurrence of first element in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 248 Views

When it is required to sort the tuple based on the occurrence of the first element, the dict.fromkeys method 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 'dict.fromkeys' method will return a dictionary with a specific key and a value.Below is a demonstration for the same −Exampledef sort_on_occurence(my_lst):    my_dict = {}    for i, j in my_lst:       my_dict.setdefault(i, []).append(j)    return([(i, *dict.fromkeys(j), len(j))       for i, j ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 681 Views

When it is required to remove tuples that have a duplicate first value from a given set of list of tuples, a simple 'for' loop, and the 'add' and 'append' methods can be used.Below is a demonstration for the same −Examplemy_input = [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), (45.324, 'You are the best.')] visited_data = set() my_output_list = [] for a, b in my_input:    if not a in visited_data:       visited_data.add(a)       my_output_list.append((a, b)) print("The list of tuple is : ") print(my_input) print("The list of tuple after removing ...

Read More

Modifying tuple contents with list in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 287 Views

When it is required to modify the list of tuple, the 'zip' method and the list comprehension can be used.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.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.Below is a demonstration for the same −Examplemy_list_1 = [('Hi', 1), ('there', 2), ('Jane', 3)] my_list_2 = [45, ...

Read More

Split tuple into groups of n in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 627 Views

When it is required to split the tuple into 'n' groups, the list comprehension can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.The list comprehension is a shorthand to iterate through the list and perform operations on it.Below is a demonstration for the same −Examplemy_tuple = (12, 34, 32, 41, 56, 78, 9, 0, 87, 53, 12, 45, 12, 6) print ("The tuple is ...

Read More

Selective value selection in list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 188 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

Update a list of tuples using another list in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 369 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 −Examplefrom 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 in ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 473 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 −Examplemy_tuple = [('a', 130), ('b', 230), ('c', 25), ('z', 654), ('f', 69)] print("The list of ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 551 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 −Examplemy_list = [('. ', 62), ('Mark', 5),    ('Paul.', 21), ('.....', 0),    ('-Jane', ...

Read More

Sort list of tuples by specific ordering in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 395 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 −Exampledef 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) print("The ...

Read More

Python Program to Shuffle Deck of Cards

AmitDiwan
AmitDiwan
Updated on 11-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 show the data.Below is a demonstration for the same −Exampleimport 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 of ...

Read More
Showing 731–740 of 8,547 articles
« Prev 1 72 73 74 75 76 855 Next »
Advertisements