
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 26504 Articles for Server Side Programming

243 Views
When it is required to display cross pattern pairs in list, a list comprehension and the ‘*’ operator are used.Below is a demonstration of the same −Examplemy_list_1 = [14, 35, 26] my_list_2 = [36, 24, 12] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) result = [i * j for j in my_list_1 for i in my_list_2] print ("The result is :") print(result)OutputThe first list is : [14, 35, 26] The second list is : [36, 24, 12] The result is : [504, 336, 168, 1260, 840, 420, 936, 624, 312]ExplanationTwo lists are ... Read More

287 Views
When it is required to display nearest occurrence between two elements in a list, a method is defined that takes three parameters. It uses the ‘not in’ operator, and the list comprehension to determine the result.Below is a demonstration of the same −Exampledef nearest_occurence_list(my_list, x, y): if x not in my_list or y not in my_list: return -1 x_index = [index for index in range(len(my_list)) if my_list[index] == x] y_index = my_list.index(y) min_dist = 1000000 result = None for element in x_index: if abs(element - y_index) < min_dist: ... Read More

192 Views
When it is required to filter tuple with integers, a simple iteration and the ‘not’ operator and the ‘isinstance’ method is used.ExampleBelow is a demonstration of the same −my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): temp = False break ... Read More

269 Views
When it is required to extract tuples with elements in a given range, the filter and lambda methods are used.ExampleBelow is a demonstration of the same −my_list = [(13, 15, 17), (25, 56), (13, 21, 19 ), (44, 14)] print("The list is :") print(my_list) beg, end = 13, 22 my_result = list(filter(lambda sub : all(element >= beg and element

307 Views
When it is required to extract ‘K’th element of every ‘N’th tuple in a list, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [(54, 51, 23), (73, 24, 47), (24, 33, 72), (64, 27, 18), (63, 24, 67), (12, 25, 77), (31, 39, 80), (33, 55, 78)] print("The list is :") print(my_list) K = 1 print("The value of K is :") print(K) N = 3 print("The value of N is :") print(N) my_result = [] for index in range(0, len(my_list), N): my_result.append(my_list[index][K]) ... Read More

189 Views
When it is required to extract ordered tuples, a list comprehension, the ‘sorted’ method, the ‘tuple’ method and the ‘==’ operator is used.ExampleBelow is a demonstration of the same −my_list = [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] print("The list is :") print(my_list) my_result = [element for element in my_list if tuple(sorted(element)) == element] print("The result is :") print(my_result)OutputThe list is : [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] The result is : [(13, 24, 56)]ExplanationA list of integers ... Read More

230 Views
When it is required to concatenate the rear elements in a tuple list, a list comprehension and the ‘join’ methods are used.ExampleBelow is a demonstration of the same −my_tuple = [(13, 42, "Will"), (48, "is a"), ("good boy", )] print("The tuple is : " ) print(my_tuple) my_result = " ".join([sub[-1] for sub in my_tuple]) print("The result is : " ) print(my_result)OutputThe list is : [(13, 42, 'Will'), (48, 'is a'), ('good boy', )] The concatenated list is : Will is a good boyExplanationA list of tuple is defined and is displayed on the console.A list comprehension and ... Read More

183 Views
When it is required to find the ‘K’th even element in a list, a list comprehension and the ‘%’ operator is used.ExampleBelow is a demonstration of the same −my_list = [14, 63, 28, 32, 18, 99, 13, 61] print("The list is :") print(my_list) K = 3 print("The value of K is :") print(K) my_result = [element for element in my_list if element % 2 == 0][K] print("The result is :") print(my_result)OutputThe list is : [14, 63, 28, 32, 18, 99, 13, 61] The value of K is : 3 The result is : 18ExplanationA list of ... Read More

179 Views
When it is required to split a list based on the next larger value, a list comprehension, the ‘iter’ method and the ‘islice’ methods are used.ExampleBelow is a demonstration of the same −from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87, 11] print("The list is :") print(my_list) length_to_split = [2, 5, 3] print("The split length list is :") print(length_to_split) temp = iter(my_list) my_result = [list(islice(temp, element)) for element in length_to_split] print("The result is :") print(my_result)OutputThe list is : [11, 22, 33, 34, 45, 26, 87, 11] The split length list is : ... Read More

578 Views
When it is required to extract element from ranges in a list, a simple iteration and the ‘extend’ method is used.ExampleBelow is a demonstration of the same −my_list = [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] print("The list is :") print(my_list) range_list = [(12, 14), (17, 18), (22, 28)] print("The list is :") print(range_list) my_result = [] for element in range_list: my_result.extend(my_list[element[0] : element[1] + 1]) print("The result is :") print(my_result)OutputThe list is : [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, ... Read More