
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 33676 Articles for Programming

218 Views
When it is required to convert a list to a customized overlapping nested list, an iteration along with the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [31, 25, 36, 76, 73, 89, 91, 100] print("The list is :") print(my_list) my_step, my_size = 3, 4 my_result = [] for index in range(0, len(my_list), my_step): my_result.append(my_list[index: index + my_size]) print("The result is :") print(my_result)OutputThe list is : [31, 25, 36, 76, 73, 89, 91, 100] The result is : [[31, 25, 36, 76], [76, 73, 89, 91], [91, 100]]ExplanationA list ... Read More

481 Views
When it is required to get indices of sign change in a list, a simple iteration along with ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] > 0 and my_list[index + 1] < 0 or my_list[index] < 0 and my_list[index + 1] < 0: my_result.append(index) print("The result is :") print(my_result)OutputThe list is : [71, 24, -34, -25, -76, 87, 29, -60, ... Read More

226 Views
When it is required to restrict the tuples by frequency of first element’s value, a simple ‘if’ condition along with an iteration and ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56),(73, 84), (91, 40), (40, 83), (13, 27)] print("The list is :") print(my_list) my_key = 1 my_result = [] mems = dict() for sub in my_list: if sub[0] not in mems.keys(): mems[sub[0]] = 1 else: mems[sub[0]] += 1 if mems[sub[0]]

392 Views
When it is required to find the starting index of all nested lists, a simple iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [[51], [91, 22, 36, 44], [25, 25], [46, 67, 78, 82, 69, 29], [ 7, 5]] print("The list is :") print(my_list) my_result = [] my_len = 0 for sub in my_list: my_result.append(my_len) my_len += len(sub) print("The initial element indices are :") print(my_result)OutputThe list is : [[51], [91, 22, 36, 44], [25, 25], [46, 67, 78, 82, 69, 29], [7, 5]] The ... Read More

364 Views
When it is required to check if a list contains particular digits, the ‘join’ method, and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list = [415, 133, 145, 451, 154] print("The list is :") print(my_list) my_digits = [1, 4, 5, 3] digit_string = ''.join([str(ele) for ele in my_digits]) all_elems = ''.join([str(ele) for ele in my_list]) my_result = True for element in all_elems: for ele in element: if ele not in digit_string: my_result = False ... Read More

654 Views
When it is required to find the total equal pairs in a list, the ‘set’ operator and the ‘//’ operator along with an iteration can be used.ExampleBelow is a demonstration of the samemy_list = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(my_list) all_elems = set(my_list) my_result = 0 for elements in all_elems: my_result += my_list.count(elements) // 2 print("The total pairs are :") print(my_result)OutputThe list is : [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] ... Read More

193 Views
When it is required to return rows that have an element at a specified index, a simple iteration and the ‘append’ function can be used.ExampleBelow is a demonstration of the samemy_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] for index in range(len(my_list_1)): if my_list_1[index][my_key] == my_list_2[index][my_key]: my_result.append(my_list_1[index]) my_result.append(my_list_1[index]) print("The ... Read More

177 Views
When it is required to find the next nearest element in a matrix, a method is defined tat iterates through the list and places a specific condition. This method is called and the results are displayed.ExampleBelow is a demonstration of the samedef get_nearest_elem(my_list, x, y, my_key): for index, row in enumerate(my_list[x:]): for j, elem in enumerate(row): if elem == my_key and j > y: return index + x, j return -1, -1 my_list = ... Read More

319 Views
When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append(1 - len(set(sub)) / len(sub)) print("The result is :") print(my_result)OutputThe list is : [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] The result is : [0, 1, 0]ExplanationA list of ... Read More

366 Views
When it is required to change the signs of the elements in a list of tuple, a simple iteration, the ‘abs’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append((abs(sub[0]), -abs(sub[1]))) print("The result is :") print(my_result)OutputThe list is : [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] The result is : [(51, -11), (24, -24), (11, -42), (12, -45), (45, ... Read More