
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
Server Side Programming Articles - Page 952 of 2650

392 Views
When it is required to concatenate tuple to a dictionary key, a list comprehension and the ‘join’ attribute are used.ExampleBelow is a demonstration of the same −my_list = [(("pyt", "is", "best"), 10), (("pyt", "cool"), 1), (("pyt", "is", "fun"), 15)] print("The list is :") print(my_list) my_result = {} for sub_list in my_list: my_result[" ".join(sub_list[0])] = sub_list[1] print("The result is :") print(my_result)OutputThe list is : [(('pyt', 'is', 'best'), 10), (('pyt', 'cool'), 1), (('pyt', 'is', 'fun'), 15)] The result is : {'pyt is best': 10, 'pyt cool': 1, 'pyt is fun': 15}ExplanationA list of tuple is ... Read More

381 Views
When it is required to restrict elements frequency in a list, a simple iteration is used along with the ‘append’ method.ExampleBelow is a demonstration of the same −from collections import defaultdict my_list = [11, 14, 15, 14, 11, 14, 14, 15, 15, 16] print("The list is :") print(my_list) my_dict = {14 : 3, 11 : 1, 16 : 1, 15 : 2} print("The dictionary is :") print(my_dict) my_result = [] my_def_dict = defaultdict(int) for element in my_list: my_def_dict[element] += 1 if my_def_dict[element] > my_dict[element]: ... Read More

311 Views
When it is required to list elements grouping in a matrix, a simple iteration, the ‘pop’ method, list comprehension and ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_list = [[14, 62], [51, 23], [12, 62], [78, 87], [41, 14]] print("The list is :") print(my_list) check_list = [14, 12, 41, 62] print("The list is :") print(check_list) my_result = [] while my_list: sub_list_1 = my_list.pop() sub_list_2 = [element for element in check_list if element not in sub_list_1] try: ... Read More

264 Views
When it is required to convert tuple into list by adding the given string after every element, the list comprehension is used.ExampleBelow is a demonstration of the same −my_tuple = ((15, 16), (71), 42, 99) print("The tuple is :") print(my_tuple) K = "Pyt" print("The value of K is :") print(K) my_result = [element for sub in my_tuple for element in (sub, K)] print("The result is :") print(my_result)OutputThe tuple is : ((15, 16), 71, 42, 99) The value of K is : Pyt The result is : [(15, 16), 'Pyt', 71, 'Pyt', 42, 'Pyt', 99, 'Pyt']ExplanationA tuple ... Read More

235 Views
When it is required to cross join every ‘K’th element, a method is defined that uses iteration and fetches the index as output.ExampleBelow is a demonstration of the same −def merge_pair_elem(my_list_1, my_list_2, K): index_1 = 0 index_2 = 0 while(index_1 < len(my_list_1)): for i in range(K): yield my_list_1[index_1] index_1 += 1 for i in range(K): ... Read More

361 Views
When it is required to find the character position of ‘K’th word from a list of strings, a list comprehension along with enumerate is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [element[0] for sub in enumerate(my_list) for element in enumerate(sub[1])] my_result = my_result[K] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The value of K is : 15 The result is : 2ExplanationA list of strings ... Read More

475 Views
When it is required to replace all characters of a list except a given character, a list comprehension, and the ‘==’ operator are used.ExampleBelow is a demonstration of the same −my_list = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is :") print(my_list) replace_char = '$' retain_char = 'P' my_result = [element if element == retain_char else replace_char for element in my_list] print("The result is :") print(my_result)OutputThe list is : ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] The result is : ['P', '$', '$', '$', '$', '$', 'P', '$', 'P']ExplanationA ... Read More

181 Views
When it is required to concatenate strings around ‘K’, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = ["python", "+", 'is', 'fun', "+", 'to', 'learn'] print("The list is :") print(my_list) K = "+" print("The value of K is :") print(K) my_result = [] index = 0 while index < len(my_list): element = my_list[index] if (index < len(my_list) - 1) and my_list[index + 1] == K: element = element + K + my_list[index + 2] ... Read More

653 Views
When it is required to sum the consecutive numbers with overlapping elements in lists, a list comprehension, list slicing, concatenation operator and ‘zip’ methods are used.ExampleBelow is a demonstration of the same −my_list = [41, 27, 53, 12, 29, 32, 16] print("The list is :") print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] print("The result is :") print(my_result)OutputThe list is : [41, 27, 53, 12, 29, 32, 16] The result is : [68, 80, 65, 41, 61, 48, 57]ExplanationA list of integers is defined and is displayed on the console.A list ... Read More

264 Views
When it is required to get the indices of each element of one list in another list, a simple iteration and the enumerate attribute along with the ‘setdefault’ method is used.It also uses list comprehension and the ‘get’ method is used.ExampleBelow is a demonstration of the same −my_list = [14, 52, 23, 47, 18, 23, 12, 54, 43, 22, 28, 13] print("The list is :") print(my_list) my_list_2 = [17, 52, 13] print("The second list is :") print(my_list_2) element_indices = dict() for index, value in enumerate(my_list): element_indices.setdefault(value, []).append(index) my_result = [element_indices.get(index, [None]) for index ... Read More