Python Articles

Page 19 of 854

Keys associated with Values in Dictionary in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 469 Views

When it is required to find the keys associated with specific values in a dictionary, the ‘index’ method can be used.Below is the demonstration of the same −Examplemy_dict ={"Hi":100, "there":121, "Mark":189} print("The dictionary is :") print(my_dict) dict_key = list(my_dict.keys()) print("The keys in the dictionary are :") print(dict_key) dict_val = list(my_dict.values()) print("The values in the dictionary are :") print(dict_val) my_position = dict_val.index(100) print("The value at position 100 is : ") print(dict_key[my_position]) my_position = dict_val.index(189) print("The value at position 189 is") print(dict_key[my_position])OutputThe dictionary is : {'Hi': 100, 'there': 121, 'Mark': 189} The keys in the dictionary are : ['Hi', ...

Read More

Maximum and Minimum K elements in Tuple using Python

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

When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.Below is the demonstration of the same −Examplemy_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result = [] my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp):    if idx < K or idx >= len(temp) - K:       my_result.append(val) ...

Read More

Create a list of tuples from given list having number and its cube in each tuple using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 956 Views

When it is required to create a list from a given list that have a number and its cube, the list comprehension can be used.Below is the demonstration of the same −Examplemy_list = [32, 54, 47, 89] print("The list is ") print(my_list) my_result = [(val, pow(val, 3)) for val in my_list] print("The result is ") print(my_result)OutputThe list is [32, 54, 47, 89] The result is [(32, 32768), (54, 157464), (47, 103823), (89, 704969)]ExplanationA list is defined, and is displayed on the console.The list comprehension is used to iterate through the list, and use the ‘pow’ method to get the ...

Read More

Closest Pair to Kth index element in Tuple using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 405 Views

When it is required to find the closest pair to the Kth index element in a tuple, the ‘enumerate’ method can be used along with ‘abs’ method.Below is the demonstration of the same −Examplemy_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print("The list is : ") print(my_list) my_tuple = (17, 23) print("The tuple is ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) min_diff, my_result = 999999999, None for idx, val in enumerate(my_list):    diff = abs(my_tuple[K - 1] - val[K - 1])    if diff < ...

Read More

Join Tuples if similar initial element in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 568 Views

When it is required to join tuples if they contain a similar initial element, a simple ‘for’ loop and an ‘of’ condition can be used. To store elements to one list, the ‘extend’ method can be used.Below is the demonstration of the same −Examplemy_list = [(43, 15), (66, 98), (64, 80), (14, 9), (47, 17)] print("The list is : ") print(my_list) my_result = [] for sub in my_list:    if my_result and my_result[-1][0] == sub[0]:       my_result[-1].extend(sub[1:])    else:       my_result.append([ele for ele in sub]) my_result = list(map(tuple, my_result)) print("The extracted elements are ...

Read More

Extract digits from Tuple list Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 738 Views

When it is required to extract digits from a list of tuple, list comprehension can be used.Below is the demonstration of the same −Examplemy_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] print("The list is : ") print(my_list) N = 2 print("The value of N is ") print(N) my_result = [sub for sub in my_list if all(len(str(ele)) == N for ele in sub)] print("The extracted tuples are : " ) print(my_result)OutputThe list is : [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] The value of N is 2 The extracted tuples are ...

Read More

Remove Tuples of Length K in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 579 Views

When it is required to remove the tuples of a specific length ‘K’, list comprehension can be used.Below is the demonstration of the same −Examplemy_list = [(32, 51), (22, 13 ), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is : ") print(my_result)OutputThe list is : [(32, 51), (22, 13), (94, 65, 77), (70, ), (80, 61, 13, 17)] The value of K is 1 ...

Read More

Python program to Order Tuples using external List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 381 Views

When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.Below is the demonstration of the same −Examplemy_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is : ") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is :") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple list is : ") print(my_result)OutputThe list of tuple is : [('Mark', 34), ('Will', 91), ('Rob', 23)] The ordered list is : ['Will', 'Mark', 'Rob'] The ordered tuple list is : ...

Read More

Flatten tuple of List to tuple in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 382 Views

When it is required to flatten tuple of a list to tuple, a method is defined, that takes input as tuple.The tuple is iterated over, and the same method is called on it over and over again until the result is obtained.Below is the demonstration of the same −Exampledef flatten_tuple(my_tuple):    if isinstance(my_tuple, tuple) and len(my_tuple) == 2 and not isinstance(my_tuple[0], tuple):       my_result = [my_tuple]       return tuple(my_result)    my_result = []    for sub in my_tuple:       my_result += flatten_tuple(sub)    return tuple(my_result) my_tuple = ((35, 46), ((67, 70), ...

Read More

Convert Nested Tuple to Custom Key Dictionary in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 723 Views

When it is required to convert a nested tuple into a customized key dictionary, the list comprehension can be used.Below is the demonstration of the same −Examplemy_tuple = ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) print("Thw tuple is : ") print(my_tuple) my_result = [{'key': sub[0], 'value': sub[1], 'id': sub[2]}    for sub in my_tuple] print("The converted dictionary is : ") print(my_result)OutputThw tuple is : ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) The converted dictionary is : [{'key': 6, 'value': 'Will', 'id': 13}, {'key': 2, 'value': 'Mark', 'id': 15}, {'key': 9, 'value': 'Rob', 'id': ...

Read More
Showing 181–190 of 8,532 articles
« Prev 1 17 18 19 20 21 854 Next »
Advertisements