When it is required to construct equi-digit tuples, the ‘//’ operator and the list slicing is used.ExampleBelow is a demonstration of the samemy_list = [5613, 1223, 966143, 890, 65, 10221] print("The list is :") print(my_list) my_result = [] for sub in my_list: mid_index = len(str(sub)) // 2 element_1 = str(sub)[:mid_index] element_2 = str(sub)[mid_index:] my_result.append((int(element_1), int(element_2))) print("The resultant list is :") print(my_result)OutputThe list is : [5613, 1223, 966143, 890, 65, 10221] The resultant list is : [(56, 13), (12, 23), (966, 143), (8, 90), ... Read More
When it is required to omit K length rows, a simple iteration and the ‘len’ method along with ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]] print("The list is :") print(my_list) my_k = 2 print("The value of K is ") print(my_k) my_result = [] for row in my_list: if len(row) != my_k : my_result.append(row) print("The resultant list is :") print(my_result)OutputThe list is : [[41, 7], [8, 10, 12, 8], [10, 11], [6, ... Read More
When it is required to extract rows with common difference elements, an iteration and a flag value is used.ExampleBelow is a demonstration of the samemy_list = [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, 10]] print("The list is :") print(my_list) my_result = [] for row in my_list: temp = True for index in range(0, len(row) - 1): if row[index + 1] - row[index] != row[1] - row[0]: temp = False ... Read More
When it is required to compute a polynomial equation, a simple iteration along with the ‘*’ operator is used.ExampleBelow is a demonstration of the samemy_list = [3, -6, 3, -1, 23, -11, 0, -8] print("The list is :") print(my_list) x = 3 my_list_length = len(my_list) my_result = 0 for i in range(my_list_length): my_sum = my_list[i] for j in range(my_list_length - i - 1): my_sum = my_sum * x my_result = my_result + my_sum print("The result is :") print(my_result)OutputThe list ... Read More
When it is required to test if a tuple list contains a single element, a flag value and a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list: flag = True for element in sub: if element != my_list[0][0]: flag = False break if not flag: my_result = False break if(flag == True): ... Read More
When it is required to redistribute the trimmed values, a list comprehension and the ‘/’ operator are used.ExampleBelow is a demonstration of the samemy_list = [11, 26, 24, 75, 96, 37, 48, 29, 93] print("The list is :") print(my_list) key = 2 print("The value of key is") print(key) full_sum = sum(my_list) trimmed_list = my_list[key:len(my_list) - key] trim_sum = sum(trimmed_list) add_value = (full_sum - trim_sum) / len(trimmed_list) result = [ele + add_value for ele in trimmed_list] print("The resultant list is:") print(result)OutputThe list is : [11, 26, 24, 75, 96, 37, 48, 29, 93] The value of ... Read More
When it is required to get the count of consecutive identical elements in a list, an iteration, the ‘append’ method, and the ‘set’ method are used.ExampleBelow is a demonstration of the samemy_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: my_result.append(my_list[index]) my_result = len(list(set(my_result))) print("The result is :") print(my_result)OutputThe list is : [24, 24, 24, 15, 15, 64, 64, 71, 13, ... Read More
When it is required to give the rows with all list elements, a flag value, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True for element in sub_list: if element not in row: flag = False ... Read More
When it is required to filter rows without the soace strings, a list comprehension, regular expression, the ‘not’ operator and the ‘any’ method are used.ExampleBelow is a demonstration of the sameimport re my_list = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The list is :") print(my_list) my_result = [row for row in my_list if not any(bool(re.search(r"\s", element)) for element in row)] print("The resultant list is :") print(my_result)OutputThe list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]ExplanationThe required packages are imported ... Read More
When it is required to find equidistant consecutive character strings, a list comprehension, the ‘all’ operator and the ‘ord’ method are used.ExampleBelow is a demonstration of the samemy_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms'] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(ord(sub[index + 1]) - ord(sub[index]) == ord(sub[1]) - ord(sub[0]) for index in range(0, len(sub) - 1))] print("The resultant list is :") print(my_result)OutputThe list is : ['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms'] The resultant list is : ['abc', 'mpsv', 'agms']ExplanationA list of string values is defined and is displayed on ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP