Python Articles

Page 19 of 854

Python counter and dictionary intersection example

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 350 Views

When it is required to demonstrate a counter and dictionary intersection, the Counter and dictionary can be used.Below is the demonstration of the same −Examplefrom collections import Counter def make_string(str_1, str_2):    dict_one = Counter(str_1)    dict_two = Counter(str_2)    result = dict_one & dict_two    return result == dict_one string_1 = 'Hi Mark' string_2 = 'how are yoU' print("The first string is :") print(string_1) print("The second string is :") print(string_2) if (make_string(string_1, string_2)==True):    print("It is possible") else:    print("It is not possible")OutputThe first string is : Hi Mark The second string is : how are ...

Read More

Python dictionary, set and counter to check if frequencies can become same

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 401 Views

When it is required to check if the frequency of a dictionary, set and counter are same, the Counter package is imported and the input is converted into a ‘Counter’. The values of a dictionary are converted to a ‘set’ and then to a list. Based on the length of the input, the output is displayed on the console.Below is the demonstration of the same −Examplefrom collections import Counter def check_all_same(my_input):    my_dict = Counter(my_input)    input_2 = list(set(my_dict.values()))    if len(input_2)>2:       print('The frequencies are not same')    elif len (input_2)==2 and input_2[1]-input_2[0]>1:       print('The ...

Read More

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
Showing 181–190 of 8,532 articles
« Prev 1 17 18 19 20 21 854 Next »
Advertisements