
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

672 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 −Example Live Demomy_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 ... Read More

517 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 −Example Live Demomy_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 ... Read More

357 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 −Example Live Demomy_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

2K+ Views
In Python, both tuples and lists are used to store data in sequence. Python provides built-in functions and operators that allow us to easily add elements of a tuple into a list and elements of a list into a tuple. In this article, we will explain various methods to perform these conversions, along with example codes. Scenario 1 You are given a list and a tuple as input, your task is to add tuple into the list and print the list as output. For example: # Input List and Tuple my_list = [3, 6, 9, 45, 66] my_tup = ... Read More

888 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 −Example Live Demomy_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 ... Read More

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 −Example Live Demomy_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: ... Read More

373 Views
When it is required to find the size of a tuple, the ‘sizeof’ method can be used.Below is the demonstration of the same −Example Live Demoimport sys tuple_1 = ("A", 1, "B", 2, "C", 3) tuple_2 = ("Java", "Lee", "Code", "Mark", "John") tuple_3 = ((1, "Bill"), ( 2, "Ant"), (3, "Fox"), (4, "Cheetah")) print("The first tuple is :") print(tuple_1) print("The second tuple is :") print(tuple_2) print("The third tuple is :") print(tuple_3) print("Size of first tuple is : " + str(sys.getsizeof(tuple_1)) + " bytes") print("Size of second tuple is : " + str(sys.getsizeof(tuple_2)) + " bytes") print("Size of third tuple is: ... Read More

427 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 −Example Live Demomy_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 : ... Read More

351 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 −Example Live Demofrom 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: ... Read More

314 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 −Example Live Demofrom 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 ... Read More