Description,Link,Code,Test_Case,Merge Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to swap first # and last element of a list # Swap function def swapList(newList): size = len(newList) # Swapping temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to swap first # and last element of a list # Swap function def swapList(newList): size = len(newList) # Swapping temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to swap first # and last element of a list # Swap function def swapList(newList): newList[0], newList[-1] = newList[-1], newList[0] return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to swap first # and last element of a list # Swap function def swapList(newList): newList[0], newList[-1] = newList[-1], newList[0] return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to swap first # and last element of a list # Swap function def swapList(list): # Storing the first and last element # as a pair in a tuple variable get get = list[-1], list[0] # unpacking those elements list[0], list[-1] = get return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to swap first # and last element of a list # Swap function def swapList(list): # Storing the first and last element # as a pair in a tuple variable get get = list[-1], list[0] # unpacking those elements list[0], list[-1] = get return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to illustrate # the usage of * operand list = [1, 2, 3, 4] a, *b, c = list print(a) print(b) print(c)","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to illustrate # the usage of * operand list = [1, 2, 3, 4] a, *b, c = list print(a) print(b) print(c) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to swap first # and last element of a list # Swap function def swapList(list): start, *middle, end = list list = [end, *middle, start] return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to swap first # and last element of a list # Swap function def swapList(list): start, *middle, end = list list = [end, *middle, start] return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to interchange first and last elements in a list,https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/,"# Python3 program to swap first # and last element of a list # Swap function def swapList(list): first = list.pop(0) last = list.pop(-1) list.insert(0, last) list.append(first) return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList))","#Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12]","Python program to interchange first and last elements in a list # Python3 program to swap first # and last element of a list # Swap function def swapList(list): first = list.pop(0) last = list.pop(-1) list.insert(0, last) list.append(first) return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) #Input : [12, 35, 9, 56, 24] #Output : [24, 35, 9, 56, 12] [END]" Python program to swap two elements in a list,https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/,"# Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1))"," #Output : [19, 65, 23, 90]","Python program to swap two elements in a list # Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) #Output : [19, 65, 23, 90] [END]" Python program to swap two elements in a list,https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/,"# Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): # popping both the elements from list first_ele = list.pop(pos1) second_ele = list.pop(pos2 - 1) # inserting in each others positions list.insert(pos1, second_ele) list.insert(pos2, first_ele) return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1))"," #Output : [19, 65, 23, 90]","Python program to swap two elements in a list # Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): # popping both the elements from list first_ele = list.pop(pos1) second_ele = list.pop(pos2 - 1) # inserting in each others positions list.insert(pos1, second_ele) list.insert(pos2, first_ele) return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) #Output : [19, 65, 23, 90] [END]" Python program to swap two elements in a list,https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/,"# Python3 program to swap elements at # given positions # Swap function def swapPositions(list, pos1, pos2): # Storing the two elements # as a pair in a tuple variable get get = list[pos1], list[pos2] # unpacking those elements list[pos2], list[pos1] = get return list # Driver Code List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1))"," #Output : [19, 65, 23, 90]","Python program to swap two elements in a list # Python3 program to swap elements at # given positions # Swap function def swapPositions(list, pos1, pos2): # Storing the two elements # as a pair in a tuple variable get get = list[pos1], list[pos2] # unpacking those elements list[pos2], list[pos1] = get return list # Driver Code List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) #Output : [19, 65, 23, 90] [END]" Python program to swap two elements in a list,https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/,"# Python3 program to swap elements # at given positions # Swap function def swapPositions(lis, pos1, pos2): temp = lis[pos1] lis[pos1] = lis[pos2] lis[pos2] = temp return lis # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1))"," #Output : [19, 65, 23, 90]","Python program to swap two elements in a list # Python3 program to swap elements # at given positions # Swap function def swapPositions(lis, pos1, pos2): temp = lis[pos1] lis[pos1] = lis[pos2] lis[pos2] = temp return lis # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) #Output : [19, 65, 23, 90] [END]" Python program to swap two elements in a list,https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/,"def swapPositions(lis, pos1, pos2): for i, x in enumerate(lis): if i == pos1: elem1 = x if i == pos2: elem2 = x lis[pos1] = elem2 lis[pos2] = elem1 return lis List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : [19, 65, 23, 90]","Python program to swap two elements in a list def swapPositions(lis, pos1, pos2): for i, x in enumerate(lis): if i == pos1: elem1 = x if i == pos2: elem2 = x lis[pos1] = elem2 lis[pos2] = elem1 return lis List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1 - 1, pos2 - 1)) # This code is contributed by Edula Vinay Kumar Reddy #Output : [19, 65, 23, 90] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using replace() + list comprehension # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + list comprehension res = [sub.replace(""G"", ""-"").replace(""e"", ""G"").replace(""-"", ""e"") for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using replace() + list comprehension # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + list comprehension res = [sub.replace(""G"", ""-"").replace(""e"", ""G"").replace(""-"", ""e"") for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using replace() + join() + split() # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + join() + split() res = "", "".join(test_list) res = res.replace(""G"", ""_"").replace(""e"", ""G"").replace(""_"", ""e"").split("", "") # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using replace() + join() + split() # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + join() + split() res = "", "".join(test_list) res = res.replace(""G"", ""_"").replace(""e"", ""G"").replace(""_"", ""e"").split("", "") # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using regex # Initializing list import re test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using regex res = [re.sub(""-"", ""e"", re.sub(""e"", ""G"", re.sub(""G"", ""-"", sub))) for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using regex # Initializing list import re test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using regex res = [re.sub(""-"", ""e"", re.sub(""e"", ""G"", re.sub(""G"", ""-"", sub))) for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Python len() li = [10, 20, 30] n = len(li) print(""The length of list is: "", n)"," #Output : The length of list is: 3","Python | Ways to find length of list # Python len() li = [10, 20, 30] n = len(li) print(""The length of list is: "", n) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Python code to demonstrate # length of list # using naive method # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using loop # Initializing counter counter = 0 for i in test_list: # incrementing counter counter = counter + 1 # Printing length of list print(""Length of list using naive method is : "" + str(counter))"," #Output : The length of list is: 3","Python | Ways to find length of list # Python code to demonstrate # length of list # using naive method # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using loop # Initializing counter counter = 0 for i in test_list: # incrementing counter counter = counter + 1 # Printing length of list print(""Length of list using naive method is : "" + str(counter)) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Python program to demonstrate working # of len() a = [] a.append(""Hello"") a.append(""Geeks"") a.append(""For"") a.append(""Geeks"") print(""The length of list is: "", len(a))"," #Output : The length of list is: 3","Python | Ways to find length of list # Python program to demonstrate working # of len() a = [] a.append(""Hello"") a.append(""Geeks"") a.append(""For"") a.append(""Geeks"") print(""The length of list is: "", len(a)) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Python code to demonstrate # length of list # using len() and length_hint from operator import length_hint # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using len() list_len = len(test_list) # Finding length of list # using length_hint() list_len_hint = length_hint(test_list) # Printing length of list print(""Length of list using len() is : "" + str(list_len)) print(""Length of list using length_hint() is : "" + str(list_len_hint))"," #Output : The length of list is: 3","Python | Ways to find length of list # Python code to demonstrate # length of list # using len() and length_hint from operator import length_hint # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using len() list_len = len(test_list) # Finding length of list # using length_hint() list_len_hint = length_hint(test_list) # Printing length of list print(""Length of list using len() is : "" + str(list_len)) print(""Length of list using length_hint() is : "" + str(list_len_hint)) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"from operator import length_hint import time # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using loop # Initializing counter start_time_naive = time.time() counter = 0 for i in test_list: # incrementing counter counter = counter + 1 end_time_naive = str(time.time() - start_time_naive) # Finding length of list # using len() start_time_len = time.time() list_len = len(test_list) end_time_len = str(time.time() - start_time_len) # Finding length of list # using length_hint() start_time_hint = time.time() list_len_hint = length_hint(test_list) end_time_hint = str(time.time() - start_time_hint) # Printing Times of each print(""Time taken using naive method is : "" + end_time_naive) print(""Time taken using len() is : "" + end_time_len) print(""Time taken using length_hint() is : "" + end_time_hint)"," #Output : The length of list is: 3","Python | Ways to find length of list from operator import length_hint import time # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using loop # Initializing counter start_time_naive = time.time() counter = 0 for i in test_list: # incrementing counter counter = counter + 1 end_time_naive = str(time.time() - start_time_naive) # Finding length of list # using len() start_time_len = time.time() list_len = len(test_list) end_time_len = str(time.time() - start_time_len) # Finding length of list # using length_hint() start_time_hint = time.time() list_len_hint = length_hint(test_list) end_time_hint = str(time.time() - start_time_hint) # Printing Times of each print(""Time taken using naive method is : "" + end_time_naive) print(""Time taken using len() is : "" + end_time_len) print(""Time taken using length_hint() is : "" + end_time_hint) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Python code to demonstrate # length of list # using sum() # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using sum() list_len = sum(1 for i in test_list) # Printing length of list print(""Length of list using len() is : "" + str(list_len)) print(""Length of list using length_hint() is : "" + str(list_len))"," #Output : The length of list is: 3","Python | Ways to find length of list # Python code to demonstrate # length of list # using sum() # Initializing list test_list = [1, 4, 5, 7, 8] # Printing test_list print(""The list is : "" + str(test_list)) # Finding length of list # using sum() list_len = sum(1 for i in test_list) # Printing length of list print(""Length of list using len() is : "" + str(list_len)) print(""Length of list using length_hint() is : "" + str(list_len)) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# python code to find the length # of list using enumerate function list1 = [1, 4, 5, 7, 8] s = 0 for i, a in enumerate(list1): s += 1 print(s)"," #Output : The length of list is: 3","Python | Ways to find length of list # python code to find the length # of list using enumerate function list1 = [1, 4, 5, 7, 8] s = 0 for i, a in enumerate(list1): s += 1 print(s) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"from collections import Counter # Initializing list test_list = [1, 4, 5, 7, 8] # Finding length of list using Counter() list_len = sum(Counter(test_list).values()) print(""Length of list using Counter() is:"", list_len) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : The length of list is: 3","Python | Ways to find length of list from collections import Counter # Initializing list test_list = [1, 4, 5, 7, 8] # Finding length of list using Counter() list_len = sum(Counter(test_list).values()) print(""Length of list using Counter() is:"", list_len) # This code is contributed by Edula Vinay Kumar Reddy #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Define the list to be used for the demonstration test_list = [1, 4, 5, 7, 8] # Calculate the length of the list using a list comprehension and the sum function # The list comprehension generates a sequence of ones for each element in the list # The sum function then sums all the ones to give the length of the list length = sum(1 for _ in test_list) # Print the length of the list print(""Length of list using list comprehension is:"", length)"," #Output : The length of list is: 3","Python | Ways to find length of list # Define the list to be used for the demonstration test_list = [1, 4, 5, 7, 8] # Calculate the length of the list using a list comprehension and the sum function # The list comprehension generates a sequence of ones for each element in the list # The sum function then sums all the ones to give the length of the list length = sum(1 for _ in test_list) # Print the length of the list print(""Length of list using list comprehension is:"", length) #Output : The length of list is: 3 [END]" Python | Ways to find length of list,https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/,"# Define a function to count the number of elements in a list using recursion def count_elements_recursion(lst): # Base case: if the list is empty, return 0 if not lst: return 0 # Recursive case: add 1 to the count of the remaining elements in the list return 1 + count_elements_recursion(lst[1:]) # Test the function with a sample list lst = [1, 2, 3, 4, 5] print(""The length of the list is:"", count_elements_recursion(lst)) # Output: The length of the list is: 5"," #Output : The length of list is: 3","Python | Ways to find length of list # Define a function to count the number of elements in a list using recursion def count_elements_recursion(lst): # Base case: if the list is empty, return 0 if not lst: return 0 # Recursive case: add 1 to the count of the remaining elements in the list return 1 + count_elements_recursion(lst[1:]) # Test the function with a sample list lst = [1, 2, 3, 4, 5] print(""The length of the list is:"", count_elements_recursion(lst)) # Output: The length of the list is: 5 #Output : The length of list is: 3 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"# Python program to find the # maximum of two numbers def maximum(a, b): if a >= b: return a else: return b # Driver code a = 2 b = 4 print(maximum(a, b))"," #Output : 4","Maximum of two numbers in Python # Python program to find the # maximum of two numbers def maximum(a, b): if a >= b: return a else: return b # Driver code a = 2 b = 4 print(maximum(a, b)) #Output : 4 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"# Python program to find the # maximum of two numbers a = 2 b = 4 maximum = max(a, b) print(maximum)"," #Output : 4","Maximum of two numbers in Python # Python program to find the # maximum of two numbers a = 2 b = 4 maximum = max(a, b) print(maximum) #Output : 4 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"# Python program to find the # maximum of two numbers # Driver code a = 2 b = 4 # Use of ternary operator print(a if a >= b else b) # This code is contributed by AnkThon"," #Output : 4","Maximum of two numbers in Python # Python program to find the # maximum of two numbers # Driver code a = 2 b = 4 # Use of ternary operator print(a if a >= b else b) # This code is contributed by AnkThon #Output : 4 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"# python code to find maximum of two numbers a = 2 b = 4 maximum = lambda a, b: a if a > b else b print(f""{maximum(a,b)} is a maximum number"")"," #Output : 4","Maximum of two numbers in Python # python code to find maximum of two numbers a = 2 b = 4 maximum = lambda a, b: a if a > b else b print(f""{maximum(a,b)} is a maximum number"") #Output : 4 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"a = 2 b = 4 x = [a if a > b else b] print(""maximum number is:"", x)"," #Output : 4","Maximum of two numbers in Python a = 2 b = 4 x = [a if a > b else b] print(""maximum number is:"", x) #Output : 4 [END]" Maximum of two numbers in Python,https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar,"# Python program to find the # maximum of two numbers a = 2 b = 4 x = [a, b] x.sort() print(x[-1])"," #Output : 4","Maximum of two numbers in Python # Python program to find the # maximum of two numbers a = 2 b = 4 x = [a, b] x.sort() print(x[-1]) #Output : 4 [END]" Minimum of two numbers in Python,https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/,"# Python program to find the # minimum of two numbers def minimum(a, b): if a <= b: return a else: return b # Driver code a = 2 b = 4 print(minimum(a, b))","Input: a = 2, b = 4 Output: 2","Minimum of two numbers in Python # Python program to find the # minimum of two numbers def minimum(a, b): if a <= b: return a else: return b # Driver code a = 2 b = 4 print(minimum(a, b)) Input: a = 2, b = 4 Output: 2 [END]" Minimum of two numbers in Python,https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/,"# Python program to find the # minimum of two numbers a = 2 b = 4 minimum = min(a, b) print(minimum)","Input: a = 2, b = 4 Output: 2","Minimum of two numbers in Python # Python program to find the # minimum of two numbers a = 2 b = 4 minimum = min(a, b) print(minimum) Input: a = 2, b = 4 Output: 2 [END]" Minimum of two numbers in Python,https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/,"a = 2 b = 4 print(sorted([a, b])[0])","Input: a = 2, b = 4 Output: 2","Minimum of two numbers in Python a = 2 b = 4 print(sorted([a, b])[0]) Input: a = 2, b = 4 Output: 2 [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# python code to Check if element exists in list or not lst = [1, 6, 3, 5, 3, 4] # checking if element 7 is present # in the given list or not i = 7 # if element present then return # exist otherwise not exist if i in lst: print(""exist"") else: print(""not exist"") # this code is contributed by gangarajula laxmi","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # python code to Check if element exists in list or not lst = [1, 6, 3, 5, 3, 4] # checking if element 7 is present # in the given list or not i = 7 # if element present then return # exist otherwise not exist if i in lst: print(""exist"") else: print(""not exist"") # this code is contributed by gangarajula laxmi Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# Initializing list test_list = [1, 6, 3, 5, 3, 4] # Checking if 4 exists in list for i in test_list: if i == 4: print(""Element Exists"")","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # Initializing list test_list = [1, 6, 3, 5, 3, 4] # Checking if 4 exists in list for i in test_list: if i == 4: print(""Element Exists"") Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# Initializing list test_list = [1, 6, 3, 5, 3, 4] # Checking if 4 exists in list # using in if 4 in test_list: print(""Element Exists"")","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # Initializing list test_list = [1, 6, 3, 5, 3, 4] # Checking if 4 exists in list # using in if 4 in test_list: print(""Element Exists"") Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# Initializing list test_list = [1, 6, 3, 5, 3, 4] result = any(item in test_list for item in test_list) print(""Does string contain any list element : "" + str(bool(result)))","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # Initializing list test_list = [1, 6, 3, 5, 3, 4] result = any(item in test_list for item in test_list) print(""Does string contain any list element : "" + str(bool(result))) Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# Initializing list test_list = [10, 15, 20, 7, 46, 2808] print(""Checking if 15 exists in list"") # number of times element exists in list exist_count = test_list.count(15) # checking if it is more than 0 if exist_count > 0: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"")","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # Initializing list test_list = [10, 15, 20, 7, 46, 2808] print(""Checking if 15 exists in list"") # number of times element exists in list exist_count = test_list.count(15) # checking if it is more than 0 if exist_count > 0: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"") Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"from bisect import bisect_left, bisect # Initializing list test_list_set = [1, 6, 3, 5, 3, 4] test_list_bisect = [1, 6, 3, 5, 3, 4] print(""Checking if 4 exists in list ( using set() + in) : "") # Checking if 4 exists in list # using set() + in test_list_set = set(test_list_set) if 4 in test_list_set: print(""Element Exists"") print(""Checking if 4 exists in list ( using sort() + bisect_left() ) : "") # Checking if 4 exists in list # using sort() + bisect_left() test_list_bisect.sort() if bisect_left(test_list_bisect, 4) != bisect(test_list_bisect, 4): print(""Element Exists"") else: print(""Element doesnt exist"")","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list from bisect import bisect_left, bisect # Initializing list test_list_set = [1, 6, 3, 5, 3, 4] test_list_bisect = [1, 6, 3, 5, 3, 4] print(""Checking if 4 exists in list ( using set() + in) : "") # Checking if 4 exists in list # using set() + in test_list_set = set(test_list_set) if 4 in test_list_set: print(""Element Exists"") print(""Checking if 4 exists in list ( using sort() + bisect_left() ) : "") # Checking if 4 exists in list # using sort() + bisect_left() test_list_bisect.sort() if bisect_left(test_list_bisect, 4) != bisect(test_list_bisect, 4): print(""Element Exists"") else: print(""Element doesnt exist"") Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"# Initializing list test_list = [10, 15, 20, 7, 46, 2808] print(""Checking if 15 exists in list"") x = list(map(str, test_list)) y = ""-"".join(x) if y.find(""15"") != -1: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"")","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list # Initializing list test_list = [10, 15, 20, 7, 46, 2808] print(""Checking if 15 exists in list"") x = list(map(str, test_list)) y = ""-"".join(x) if y.find(""15"") != -1: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"") Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"from collections import Counter test_list = [10, 15, 20, 7, 46, 2808] # Calculating frequencies frequency = Counter(test_list) # If the element has frequency greater than 0 # then it exists else it doesn't exist if frequency[15] > 0: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"") # This code is contributed by vikkycirus","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list from collections import Counter test_list = [10, 15, 20, 7, 46, 2808] # Calculating frequencies frequency = Counter(test_list) # If the element has frequency greater than 0 # then it exists else it doesn't exist if frequency[15] > 0: print(""Yes, 15 exists in list"") else: print(""No, 15 does not exists in list"") # This code is contributed by vikkycirus Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"def element_exists(lst, element): # Try to get the index of the element in the list try: lst.index(element) # If the element is found, return True return True # If a ValueError is raised, the element is not in the list except ValueError: # Return False in this case return False # Test the function test_list = [1, 6, 3, 5, 3, 4] print(element_exists(test_list, 3)) # prints True print(element_exists(test_list, 7)) # prints False # This code is contributed by Edula Vinay Kumar Reddy","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list def element_exists(lst, element): # Try to get the index of the element in the list try: lst.index(element) # If the element is found, return True return True # If a ValueError is raised, the element is not in the list except ValueError: # Return False in this case return False # Test the function test_list = [1, 6, 3, 5, 3, 4] print(element_exists(test_list, 3)) # prints True print(element_exists(test_list, 7)) # prints False # This code is contributed by Edula Vinay Kumar Reddy Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Python | Ways to check if element exists in list,https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/,"def check_element_exists_set(lst, target): return target in set(lst) # Example test_list = [1, 6, 3, 5, 3, 4] target = 3 print(""Exists using set: "", check_element_exists_set(test_list, target))","Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4]","Python | Ways to check if element exists in list def check_element_exists_set(lst, target): return target in set(lst) # Example test_list = [1, 6, 3, 5, 3, 4] target = 3 print(""Exists using set: "", check_element_exists_set(test_list, target)) Input: 3 # Check if 3 exist or not. list = test_list = [1, 6, 3, 5, 3, 4] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python program to clear a list # using clear() method # Creating list GEEK = [6, 0, 4, 1] print(""GEEK before clear:"", GEEK) # Clearing list GEEK.clear() print(""GEEK after clear:"", GEEK)"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python program to clear a list # using clear() method # Creating list GEEK = [6, 0, 4, 1] print(""GEEK before clear:"", GEEK) # Clearing list GEEK.clear() print(""GEEK after clear:"", GEEK) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python3 code to demonstrate # clearing a list using # clear and Reinitializing # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list using clear() list1.clear() # Printing list1 after clearing print(""List1 after clearing using clear() : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using reinitialization list2 = [] # Printing list2 after reinitialization print(""List2 after clearing using reinitialization : "" + str(list2))"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python3 code to demonstrate # clearing a list using # clear and Reinitializing # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list using clear() list1.clear() # Printing list1 after clearing print(""List1 after clearing using clear() : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using reinitialization list2 = [] # Printing list2 after reinitialization print(""List2 after clearing using reinitialization : "" + str(list2)) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python3 code to demonstrate # clearing a list using # clear and Reinitializing # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list using clear() list1.clear() # Printing list1 after clearing print(""List1 after clearing using clear() : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using reinitialization list2 = [] # Printing list2 after reinitialization print(""List2 after clearing using reinitialization : "" + str(list2))"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python3 code to demonstrate # clearing a list using # clear and Reinitializing # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list using clear() list1.clear() # Printing list1 after clearing print(""List1 after clearing using clear() : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using reinitialization list2 = [] # Printing list2 after reinitialization print(""List2 after clearing using reinitialization : "" + str(list2)) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python3 code to demonstrate # clearing a list using # del method # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list1 using del del list1[:] print(""List1 after clearing using del : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using del del list2[:] print(""List2 after clearing using del : "" + str(list2))"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python3 code to demonstrate # clearing a list using # del method # Initializing lists list1 = [1, 2, 3] list2 = [5, 6, 7] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list1 using del del list1[:] print(""List1 after clearing using del : "" + str(list1)) # Printing list2 before deleting print(""List2 before deleting is : "" + str(list2)) # deleting list using del del list2[:] print(""List2 after clearing using del : "" + str(list2)) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python3 code to demonstrate # Initializing lists list1 = [1, 2, 3] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list1 while len(list1) != 0: list1.pop() print(""List1 after clearing using del : "" + str(list1))"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python3 code to demonstrate # Initializing lists list1 = [1, 2, 3] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # deleting list1 while len(list1) != 0: list1.pop() print(""List1 after clearing using del : "" + str(list1)) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Initializing list lst = [1, 2, 3, 4, 5] # Clearing list using slicing lst = lst[:0] print(lst) # Output: [] # This code is contributed by Edula Vinay Kumar Reddy"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Initializing list lst = [1, 2, 3, 4, 5] # Clearing list using slicing lst = lst[:0] print(lst) # Output: [] # This code is contributed by Edula Vinay Kumar Reddy #Output : GEEK before clear: [6, 0, 4, 1] [END]" Different ways to clear a list in Python,https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/,"# Python3 code to demonstrate # Initializing lists list1 = [1, 2, 3] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # Clearing list1 by assigning an empty list list1 = [] print(""List1 after clearing using an empty list : "" + str(list1))"," #Output : GEEK before clear: [6, 0, 4, 1]","Different ways to clear a list in Python # Python3 code to demonstrate # Initializing lists list1 = [1, 2, 3] # Printing list1 before deleting print(""List1 before deleting is : "" + str(list1)) # Clearing list1 by assigning an empty list list1 = [] print(""List1 after clearing using an empty list : "" + str(list1)) #Output : GEEK before clear: [6, 0, 4, 1] [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"# Python program to reverse an array def list_reverse(arr, size): # if only one element present, then return the array if size == 1: return arr # if only two elements present, then swap both the numbers. elif size == 2: ( arr[0], arr[1], ) = ( arr[1], arr[0], ) return arr # if more than two elements presents, then swap first and last numbers. else: i = 0 while i < size // 2: # swap present and preceding numbers at time and jump to second element after swap arr[i], arr[size - i - 1] = arr[size - i - 1], arr[i] # skip if present and preceding numbers indexes are same if (i != i + 1 and size - i - 1 != size - i - 2) and ( i != size - i - 2 and size - i - 1 != i + 1 ): arr[i + 1], arr[size - i - 2] = arr[size - i - 2], arr[i + 1] i += 2 return arr arr = [1, 2, 3, 4, 5] size = 5 print(""Original list: "", arr) print(""Reversed list: "", list_reverse(arr, size)) # This contributed by SR.Dhanush","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List # Python program to reverse an array def list_reverse(arr, size): # if only one element present, then return the array if size == 1: return arr # if only two elements present, then swap both the numbers. elif size == 2: ( arr[0], arr[1], ) = ( arr[1], arr[0], ) return arr # if more than two elements presents, then swap first and last numbers. else: i = 0 while i < size // 2: # swap present and preceding numbers at time and jump to second element after swap arr[i], arr[size - i - 1] = arr[size - i - 1], arr[i] # skip if present and preceding numbers indexes are same if (i != i + 1 and size - i - 1 != size - i - 2) and ( i != size - i - 2 and size - i - 1 != i + 1 ): arr[i + 1], arr[size - i - 2] = arr[size - i - 2], arr[i + 1] i += 2 return arr arr = [1, 2, 3, 4, 5] size = 5 print(""Original list: "", arr) print(""Reversed list: "", list_reverse(arr, size)) # This contributed by SR.Dhanush Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"lst = [10, 11, 12, 13, 14, 15] lst.reverse() print(""Using reverse() "", lst) print(""Using reversed() "", list(reversed(lst)))","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List lst = [10, 11, 12, 13, 14, 15] lst.reverse() print(""Using reverse() "", lst) print(""Using reversed() "", list(reversed(lst))) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"# Reversing a list using two-pointer approach def reverse_list(arr): left = 0 right = len(arr) - 1 while left < right: # Swap temp = arr[left] arr[left] = arr[right] arr[right] = temp left += 1 right -= 1 return arr arr = [1, 2, 3, 4, 5, 6, 7] print(reverse_list(arr))","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List # Reversing a list using two-pointer approach def reverse_list(arr): left = 0 right = len(arr) - 1 while left < right: # Swap temp = arr[left] arr[left] = arr[right] arr[right] = temp left += 1 right -= 1 return arr arr = [1, 2, 3, 4, 5, 6, 7] print(reverse_list(arr)) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"# input list lst = [10, 11, 12, 13, 14, 15] # the above input can also be given as # lst=list(map(int,input().split())) l = [] # empty list # iterate to reverse the list for i in lst: # reversing the list l.insert(0, i) # printing result print(l)","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List # input list lst = [10, 11, 12, 13, 14, 15] # the above input can also be given as # lst=list(map(int,input().split())) l = [] # empty list # iterate to reverse the list for i in lst: # reversing the list l.insert(0, i) # printing result print(l) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"# Reversing a list using slicing technique def Reverse(lst): new_lst = lst[::-1] return new_lst lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst))","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List # Reversing a list using slicing technique def Reverse(lst): new_lst = lst[::-1] return new_lst lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst)) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"original_list = [10, 11, 12, 13, 14, 15] new_list = [ original_list[len(original_list) - i] for i in range(1, len(original_list) + 1) ] print(new_list)","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List original_list = [10, 11, 12, 13, 14, 15] new_list = [ original_list[len(original_list) - i] for i in range(1, len(original_list) + 1) ] print(new_list) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"from functools import reduce original_list = [10, 11, 12, 13, 14, 15] new_list = reduce(lambda a, b: [b] + [a] if type(a) == int else [b] + a, original_list) print(new_list)","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List from functools import reduce original_list = [10, 11, 12, 13, 14, 15] new_list = reduce(lambda a, b: [b] + [a] if type(a) == int else [b] + a, original_list) print(new_list) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"def reverse_list(lst): reversed_lst = [] for i in range(len(lst) - 1, -1, -1): reversed_lst.append(lst[i]) return reversed_lst lst = [1, 2, 3, 4, 5] reversed_lst = reverse_list(lst) print(reversed_lst) # Output: [5, 4, 3, 2, 1]","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List def reverse_list(lst): reversed_lst = [] for i in range(len(lst) - 1, -1, -1): reversed_lst.append(lst[i]) return reversed_lst lst = [1, 2, 3, 4, 5] reversed_lst = reverse_list(lst) print(reversed_lst) # Output: [5, 4, 3, 2, 1] Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"# Input list my_list = [4, 5, 6, 7, 8, 9] # Initialize an empty list to store reversed values reversed_list = [] # Loop through the original list backwards for i in range(len(my_list) - 1, -1, -1): # Insert each value at the end of the reversed list reversed_list.append(my_list[i]) # Print the reversed list print(reversed_list)","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List # Input list my_list = [4, 5, 6, 7, 8, 9] # Initialize an empty list to store reversed values reversed_list = [] # Loop through the original list backwards for i in range(len(my_list) - 1, -1, -1): # Insert each value at the end of the reversed list reversed_list.append(my_list[i]) # Print the reversed list print(reversed_list) Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Reversing a List,https://www.geeksforgeeks.org/python-reversing-list/,"import numpy as np # Input list my_list = [4, 5, 6, 7, 8, 9] # Convert the list to a 1D numpy array my_array = np.array(my_list) # Reverse the order of the array reversed_array = my_array[::-1] # Convert the reversed array to a list reversed_list = reversed_array.tolist() # Print the reversed list print(reversed_list) # This code is contributed by Jyothi pinjala.","Input: arr[], size 1)if length of array is 1, then return arr.","Python | Reversing a List import numpy as np # Input list my_list = [4, 5, 6, 7, 8, 9] # Convert the list to a 1D numpy array my_array = np.array(my_list) # Reverse the order of the array reversed_array = my_array[::-1] # Convert the reversed array to a list reversed_list = reversed_array.tolist() # Print the reversed list print(reversed_list) # This code is contributed by Jyothi pinjala. Input: arr[], size 1)if length of array is 1, then return arr. [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python program to copy or clone a list # Using the Slice Operator def Cloning(li1): li_copy = li1[:] return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python program to copy or clone a list # Using the Slice Operator def Cloning(li1): li_copy = li1[:] return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using the in-built function extend() def Cloning(li1): li_copy = [] li_copy.extend(li1) return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using the in-built function extend() def Cloning(li1): li_copy = [] li_copy.extend(li1) return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using the List copy using = def Cloning(li1): li_copy = li1 return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using the List copy using = def Cloning(li1): li_copy = li1 return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# importing copy module import copy # initializing list 1 li1 = [1, 2, [3, 5], 4] # using copy for shallow copy li2 = copy.copy(li1) print(li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # importing copy module import copy # initializing list 1 li1 = [1, 2, [3, 5], 4] # using copy for shallow copy li2 = copy.copy(li1) print(li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using list comprehension def Cloning(li1): li_copy = [i for i in li1] return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using list comprehension def Cloning(li1): li_copy = [i for i in li1] return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using append() def Cloning(li1): li_copy = [] for item in li1: li_copy.append(item) return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using append() def Cloning(li1): li_copy = [] for item in li1: li_copy.append(item) return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using built-in method copy() def Cloning(li1): li_copy = [] li_copy = li1.copy() return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using built-in method copy() def Cloning(li1): li_copy = [] li_copy = li1.copy() return li_copy # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# importing copy module import copy # initializing list 1 li1 = [1, 2, [3, 5], 4] # using deepcopy for deepcopy li3 = copy.deepcopy(li1) print(li3)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # importing copy module import copy # initializing list 1 li1 = [1, 2, [3, 5], 4] # using deepcopy for deepcopy li3 = copy.deepcopy(li1) print(li3) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using list comprehension lst = [4, 8, 2, 10, 15, 18] li_copy = [i for a, i in enumerate(lst)] print(""Original List:"", lst) print(""After Cloning:"", li_copy)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using list comprehension lst = [4, 8, 2, 10, 15, 18] li_copy = [i for a, i in enumerate(lst)] print(""Original List:"", lst) print(""After Cloning:"", li_copy) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using map function lst = [4, 8, 2, 10, 15, 18] li_copy = map(int, lst) print(""Original List:"", lst) print(""After Cloning:"", *li_copy)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using map function lst = [4, 8, 2, 10, 15, 18] li_copy = map(int, lst) print(""Original List:"", lst) print(""After Cloning:"", *li_copy) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"# Python code to clone or copy a list # Using slice() function lst = [4, 8, 2, 10, 15, 18] li_copy = lst[slice(len(lst))] print(""Original List:"", lst) print(""After Cloning:"", li_copy) # This code is contributed by Pushpesh Raj."," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list # Python code to clone or copy a list # Using slice() function lst = [4, 8, 2, 10, 15, 18] li_copy = lst[slice(len(lst))] print(""Original List:"", lst) print(""After Cloning:"", li_copy) # This code is contributed by Pushpesh Raj. #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"from collections import deque original_list = [4, 8, 2, 10, 15, 18] copied_list = deque(original_list) copied_list = list(copied_list) print(""Original List:"", original_list) print(""After Cloning:"", copied_list)"," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list from collections import deque original_list = [4, 8, 2, 10, 15, 18] copied_list = deque(original_list) copied_list = list(copied_list) print(""Original List:"", original_list) print(""After Cloning:"", copied_list) #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Cloning or Copying a list,https://www.geeksforgeeks.org/python-cloning-copying-list/,"from functools import reduce def clone_list(li1): return reduce(lambda x, y: x + [y], li1, []) # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = clone_list(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) # This code is contributed by Jyothi pinjala."," #Output : Original List: [4, 8, 2, 10, 15, 18]??","Python | Cloning or Copying a list from functools import reduce def clone_list(li1): return reduce(lambda x, y: x + [y], li1, []) # Driver Code li1 = [4, 8, 2, 10, 15, 18] li2 = clone_list(li1) print(""Original List:"", li1) print(""After Cloning:"", li2) # This code is contributed by Jyothi pinjala. #Output : Original List: [4, 8, 2, 10, 15, 18]?? [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"# Python code to count the number of occurrences def countX(lst, x): count = 0 for ele in lst: if ele == x: count = count + 1 return count # Driver Code lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 8 print(""{} has occurred {} times"".format(x, countX(lst, x)))","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list # Python code to count the number of occurrences def countX(lst, x): count = 0 for ele in lst: if ele == x: count = count + 1 return count # Driver Code lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 8 print(""{} has occurred {} times"".format(x, countX(lst, x))) Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"# Python code to count the number of occurrences def countX(lst, x): return lst.count(x) # Driver Code lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 8 print(""{} has occurred {} times"".format(x, countX(lst, x)))","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list # Python code to count the number of occurrences def countX(lst, x): return lst.count(x) # Driver Code lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 8 print(""{} has occurred {} times"".format(x, countX(lst, x))) Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"from collections import Counter # declaring the list l = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] # driver program x = 3 d = Counter(l) print(""{} has occurred {} times"".format(x, d[x]))","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list from collections import Counter # declaring the list l = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] # driver program x = 3 d = Counter(l) print(""{} has occurred {} times"".format(x, d[x])) Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"import operator as op # declaring the list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] # driver program x = 2 print(f""{x} has occurred {op.countOf(l, x)} times"")","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list import operator as op # declaring the list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] # driver program x = 2 print(f""{x} has occurred {op.countOf(l, x)} times"") Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"lis = [""a"", ""d"", ""d"", ""c"", ""a"", ""b"", ""b"", ""a"", ""c"", ""d"", ""e""] occurrence = {item: lis.count(item) for item in lis} print(occurrence.get(""e""))","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list lis = [""a"", ""d"", ""d"", ""c"", ""a"", ""b"", ""b"", ""a"", ""c"", ""d"", ""e""] occurrence = {item: lis.count(item) for item in lis} print(occurrence.get(""e"")) Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"import pandas as pd # declaring the list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] count = pd.Series(l).value_counts() print(""Element Count"") print(count)","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list import pandas as pd # declaring the list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] count = pd.Series(l).value_counts() print(""Element Count"") print(count) Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] ele = 1 x = [i for i in l if i == ele] print(""the element"", ele, ""occurs"", len(x), ""times"")","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] ele = 1 x = [i for i in l if i == ele] print(""the element"", ele, ""occurs"", len(x), ""times"") Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python | Count occurrences of an element in a list,https://www.geeksforgeeks.org/python-count-occurrences-element-list/,"l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] ele = 1 x = [i for j, i in enumerate(l) if i == ele] print(""the element"", ele, ""occurs"", len(x), ""times"")","Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 ","Python | Count occurrences of an element in a list l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5] ele = 1 x = [i for j, i in enumerate(l) if i == ele] print(""the element"", ele, ""occurs"", len(x), ""times"") Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"# Python program to find the sum # and average of the list L = [4, 5, 1, 2, 9, 7, 10, 8] # variable to store the sum of # the list count = 0 # Finding the sum for i in L: count += i # divide the total elements by # number of elements avg = count / len(L) print(""sum = "", count) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python # Python program to find the sum # and average of the list L = [4, 5, 1, 2, 9, 7, 10, 8] # variable to store the sum of # the list count = 0 # Finding the sum for i in L: count += i # divide the total elements by # number of elements avg = count / len(L) print(""sum = "", count) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"# Python program to find the sum # and average of the list L = [4, 5, 1, 2, 9, 7, 10, 8] # using sum() method count = sum(L) # finding average avg = count / len(L) print(""sum = "", count) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python # Python program to find the sum # and average of the list L = [4, 5, 1, 2, 9, 7, 10, 8] # using sum() method count = sum(L) # finding average avg = count / len(L) print(""sum = "", count) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"# Python program to find the sum # and average of the list import statistics L = [4, 5, 1, 2, 9, 7, 10, 8] # using sum() method sum1 = sum(L) # finding average avg = statistics.mean(L) print(""sum = "", sum1) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python # Python program to find the sum # and average of the list import statistics L = [4, 5, 1, 2, 9, 7, 10, 8] # using sum() method sum1 = sum(L) # finding average avg = statistics.mean(L) print(""sum = "", sum1) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"import numpy as np L = [4, 5, 1, 2, 9, 7, 10, 8] # finding sum of list using numpy sum_ = np.sum(L) # finding average of list using numpy avg = np.average(L) print(""sum = "", sum_) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python import numpy as np L = [4, 5, 1, 2, 9, 7, 10, 8] # finding sum of list using numpy sum_ = np.sum(L) # finding average of list using numpy avg = np.average(L) print(""sum = "", sum_) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"def sum_avg_list(lst, n): if n == 0: return (0, 0) else: sum, avg = sum_avg_list(lst, n - 1) return (sum + lst[n - 1], avg + 1) def avg_list(lst): sum, avg = sum_avg_list(lst, len(lst)) return sum / avg lst = [4, 5, 1, 2, 9, 7, 10, 8] print(""Sum of the list: "", sum_avg_list(lst, len(lst))[0]) print(""Average of the list: "", avg_list(lst)) lst = [15, 9, 55, 41, 35, 20, 62, 49] print(""Sum of the list: "", sum_avg_list(lst, len(lst))[0]) print(""Average of the list: "", avg_list(lst))","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python def sum_avg_list(lst, n): if n == 0: return (0, 0) else: sum, avg = sum_avg_list(lst, n - 1) return (sum + lst[n - 1], avg + 1) def avg_list(lst): sum, avg = sum_avg_list(lst, len(lst)) return sum / avg lst = [4, 5, 1, 2, 9, 7, 10, 8] print(""Sum of the list: "", sum_avg_list(lst, len(lst))[0]) print(""Average of the list: "", avg_list(lst)) lst = [15, 9, 55, 41, 35, 20, 62, 49] print(""Sum of the list: "", sum_avg_list(lst, len(lst))[0]) print(""Average of the list: "", avg_list(lst)) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"from functools import reduce L = [4, 5, 1, 2, 9, 7, 10, 8] # Finding the sum using reduce() and lambda count = reduce(lambda x, y: x + y, L) # divide the total elements by number of elements avg = count / len(L) print(""sum = "", count) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python from functools import reduce L = [4, 5, 1, 2, 9, 7, 10, 8] # Finding the sum using reduce() and lambda count = reduce(lambda x, y: x + y, L) # divide the total elements by number of elements avg = count / len(L) print(""sum = "", count) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python Program to find sum and average of List in Python,https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/,"L = [4, 5, 1, 2, 9, 7, 10, 8] # Finding the sum count = sum(L) # Finding the average avg = count / len(L) print(""sum = "", count) print(""average = "", avg)","Input: [4, 5, 1, 2, 9, 7, 10, 8] Output:","Python Program to find sum and average of List in Python L = [4, 5, 1, 2, 9, 7, 10, 8] # Finding the sum count = sum(L) # Finding the average avg = count / len(L) print(""sum = "", count) print(""average = "", avg) Input: [4, 5, 1, 2, 9, 7, 10, 8] Output: [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Sum of number digits in List # using loop + str() # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using loop + str() res = [] for ele in test_list: sum = 0 for digit in str(ele): sum += int(digit) res.append(sum) # printing result print(""List Integer Summation : "" + str(res))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List # Python3 code to demonstrate # Sum of number digits in List # using loop + str() # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using loop + str() res = [] for ele in test_list: sum = 0 for digit in str(ele): sum += int(digit) res.append(sum) # printing result print(""List Integer Summation : "" + str(res)) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Sum of number digits in List # using sum() + list comprehension # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using sum() + list comprehension res = list(map(lambda ele: sum(int(sub) for sub in str(ele)), test_list)) # printing result print(""List Integer Summation : "" + str(res))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List # Python3 code to demonstrate # Sum of number digits in List # using sum() + list comprehension # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using sum() + list comprehension res = list(map(lambda ele: sum(int(sub) for sub in str(ele)), test_list)) # printing result print(""List Integer Summation : "" + str(res)) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Sum of number digits in a List # using sum() + reduce() from functools import reduce # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using sum() + reduce() res = [reduce(lambda x, y: int(x) + int(y), list(str(i))) for i in test_list] # printing result print(""List Integer Summation : "" + str(res))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List # Python3 code to demonstrate # Sum of number digits in a List # using sum() + reduce() from functools import reduce # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using sum() + reduce() res = [reduce(lambda x, y: int(x) + int(y), list(str(i))) for i in test_list] # printing result print(""List Integer Summation : "" + str(res)) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"import numpy as np # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using numpy res = np.sum([list(map(int, str(ele))) for ele in test_list], axis=1) # printing result print(""List Integer Summation : "" + str(list(res)))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List import numpy as np # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using numpy res = np.sum([list(map(int, str(ele))) for ele in test_list], axis=1) # printing result print(""List Integer Summation : "" + str(list(res))) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Sum of number digits in List # using itertools library # importing itertools library import itertools # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using itertools library res = [sum(map(int, list(itertools.chain(*str(ele))))) for ele in test_list] # printing result print(""List Integer Summation : "" + str(res)) # This code is contributed by Jyothi pinjala."," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List # Python3 code to demonstrate # Sum of number digits in List # using itertools library # importing itertools library import itertools # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # using itertools library res = [sum(map(int, list(itertools.chain(*str(ele))))) for ele in test_list] # printing result print(""List Integer Summation : "" + str(res)) # This code is contributed by Jyothi pinjala. #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"lst = [12, 67, 98, 34] def digit_sum(num): digit_sum = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def sum_of_digits_list(lst): return list(map(digit_sum, lst)) print(sum_of_digits_list(lst))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List lst = [12, 67, 98, 34] def digit_sum(num): digit_sum = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def sum_of_digits_list(lst): return list(map(digit_sum, lst)) print(sum_of_digits_list(lst)) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Sum of number digits in List,https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar,"# Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # creating an expression res = list(sum(int(digit) for digit in str(num)) for num in test_list) # printing result print(""List Integer Summation : "" + str(list(res)))"," #Output : The original list is : [12, 67, 98, 34]","Python | Sum of number digits in List # Initializing list test_list = [12, 67, 98, 34] # printing original list print(""The original list is : "" + str(test_list)) # Sum of number digits in List # creating an expression res = list(sum(int(digit) for digit in str(num)) for num in test_list) # printing result print(""List Integer Summation : "" + str(list(res))) #Output : The original list is : [12, 67, 98, 34] [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python program to multiply all values in the # list using traversal def multiplyList(myList): # Multiply elements one by one result = 1 for x in myList: result = result * x return result # Driver code list1 = [1, 2, 3] list2 = [3, 2, 4] print(multiplyList(list1)) print(multiplyList(list2))"," #Output : 6","Python | Multiply all numbers in the list # Python program to multiply all values in the # list using traversal def multiplyList(myList): # Multiply elements one by one result = 1 for x in myList: result = result * x return result # Driver code list1 = [1, 2, 3] list2 = [3, 2, 4] print(multiplyList(list1)) print(multiplyList(list2)) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python3 program to multiply all values in the # list using numpy.prod() import numpy list1 = [1, 2, 3] list2 = [3, 2, 4] # using numpy.prod() to get the multiplications result1 = numpy.prod(list1) result2 = numpy.prod(list2) print(result1) print(result2)"," #Output : 6","Python | Multiply all numbers in the list # Python3 program to multiply all values in the # list using numpy.prod() import numpy list1 = [1, 2, 3] list2 = [3, 2, 4] # using numpy.prod() to get the multiplications result1 = numpy.prod(list1) result2 = numpy.prod(list2) print(result1) print(result2) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python3 program to multiply all values in the # list using lambda function and reduce() from functools import reduce list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = reduce((lambda x, y: x * y), list1) result2 = reduce((lambda x, y: x * y), list2) print(result1) print(result2)"," #Output : 6","Python | Multiply all numbers in the list # Python3 program to multiply all values in the # list using lambda function and reduce() from functools import reduce list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = reduce((lambda x, y: x * y), list1) result2 = reduce((lambda x, y: x * y), list2) print(result1) print(result2) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python3 program to multiply all values in the # list using math.prod import math list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = math.prod(list1) result2 = math.prod(list2) print(result1) print(result2)"," #Output : 6","Python | Multiply all numbers in the list # Python3 program to multiply all values in the # list using math.prod import math list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = math.prod(list1) result2 = math.prod(list2) print(result1) print(result2) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python 3 program to multiply all numbers in # the given list by importing operator module from operator import * list1 = [1, 2, 3] m = 1 for i in list1: # multiplying all elements in the given list # using mul function of operator module m = mul(i, m) # printing the result print(m)"," #Output : 6","Python | Multiply all numbers in the list # Python 3 program to multiply all numbers in # the given list by importing operator module from operator import * list1 = [1, 2, 3] m = 1 for i in list1: # multiplying all elements in the given list # using mul function of operator module m = mul(i, m) # printing the result print(m) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python program to multiply all values in the # list using traversal def multiplyList(myList): # Multiply elements one by one result = 1 for i in range(0, len(myList)): result = result * myList[i] return result # Driver code list1 = [1, 2, 3] list2 = [3, 2, 4] print(multiplyList(list1)) print(multiplyList(list2))"," #Output : 6","Python | Multiply all numbers in the list # Python program to multiply all values in the # list using traversal def multiplyList(myList): # Multiply elements one by one result = 1 for i in range(0, len(myList)): result = result * myList[i] return result # Driver code list1 = [1, 2, 3] list2 = [3, 2, 4] print(multiplyList(list1)) print(multiplyList(list2)) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"# Python3 program to multiply all values in the # list using lambda function and accumulate() from itertools import accumulate list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = list(accumulate(list1, (lambda x, y: x * y))) result2 = list(accumulate(list2, (lambda x, y: x * y))) print(result1[-1]) print(result2[-1])"," #Output : 6","Python | Multiply all numbers in the list # Python3 program to multiply all values in the # list using lambda function and accumulate() from itertools import accumulate list1 = [1, 2, 3] list2 = [3, 2, 4] result1 = list(accumulate(list1, (lambda x, y: x * y))) result2 = list(accumulate(list2, (lambda x, y: x * y))) print(result1[-1]) print(result2[-1]) #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"def product_recursive(numbers): # base case: list is empty if not numbers: return 1 # recursive case: multiply first element by product of the rest of the list return numbers[0] * product_recursive(numbers[1:]) list1 = [1, 2, 3] product = product_recursive(list1) print(product) # Output: 6 list2 = [3, 2, 4] product = product_recursive(list2) print(product) # Output: 24"," #Output : 6","Python | Multiply all numbers in the list def product_recursive(numbers): # base case: list is empty if not numbers: return 1 # recursive case: multiply first element by product of the rest of the list return numbers[0] * product_recursive(numbers[1:]) list1 = [1, 2, 3] product = product_recursive(list1) print(product) # Output: 6 list2 = [3, 2, 4] product = product_recursive(list2) print(product) # Output: 24 #Output : 6 [END]" Python | Multiply all numbers in the list,https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/,"from functools import reduce from operator import mul list1 = [1, 2, 3] result = reduce(mul, list1) print(result)"," #Output : 6","Python | Multiply all numbers in the list from functools import reduce from operator import mul list1 = [1, 2, 3] result = reduce(mul, list1) print(result) #Output : 6 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python program to find smallest # number in a list # list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort() # printing the first element print(""Smallest element is:"", list1[0])","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python program to find smallest # number in a list # list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort() # printing the first element print(""Smallest element is:"", list1[0]) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort(reverse=True) # printing the first element print(""Smallest element is:"", list1[-1])","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort(reverse=True) # printing the first element print(""Smallest element is:"", list1[-1]) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python program to find smallest # number in a list # list of numbers list1 = [10, 20, 1, 45, 99] # printing the maximum element print(""Smallest element is:"", min(list1))","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python program to find smallest # number in a list # list of numbers list1 = [10, 20, 1, 45, 99] # printing the maximum element print(""Smallest element is:"", min(list1)) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python program to find smallest # number in a list # creating empty list list1 = [] # asking number of elements to put in list num = int(input(""Enter number of elements in list: "")) # iterating till num to append elements in list for i in range(1, num + 1): ele = int(input(""Enter elements: "")) list1.append(ele) # print maximum element print(""Smallest element is:"", min(list1))","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python program to find smallest # number in a list # creating empty list list1 = [] # asking number of elements to put in list num = int(input(""Enter number of elements in list: "")) # iterating till num to append elements in list for i in range(1, num + 1): ele = int(input(""Enter elements: "")) list1.append(ele) # print maximum element print(""Smallest element is:"", min(list1)) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python program to find smallest # number in a list l = [int(l) for l in input(""List:"").split("","")] print(""The list is "", l) # Assign first element as a minimum. min1 = l[0] for i in range(len(l)): # If the other element is min than first element if l[i] < min1: min1 = l[i] # It will change print(""The smallest element in the list is "", min1)","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python program to find smallest # number in a list l = [int(l) for l in input(""List:"").split("","")] print(""The list is "", l) # Assign first element as a minimum. min1 = l[0] for i in range(len(l)): # If the other element is min than first element if l[i] < min1: min1 = l[i] # It will change print(""The smallest element in the list is "", min1) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python code to print smallest element in the list lst = [20, 10, 20, 1, 100] print(min(lst, key=lambda value: int(value)))","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python code to print smallest element in the list lst = [20, 10, 20, 1, 100] print(min(lst, key=lambda value: int(value))) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"lst = [20, 10, 20, 1, 100] a, i = min((a, i) for (i, a) in enumerate(lst)) print(a)","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list lst = [20, 10, 20, 1, 100] a, i = min((a, i) for (i, a) in enumerate(lst)) print(a) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# Python code to print smallest element in the list from functools import reduce lst = [20, 10, 20, 15, 100] print(reduce(min, lst))","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # Python code to print smallest element in the list from functools import reduce lst = [20, 10, 20, 15, 100] print(reduce(min, lst)) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"import heapq def find_smallest(numbers): # Build a min heap using the elements in the list heap = [(x, x) for x in numbers] heapq.heapify(heap) # Return the root element (smallest element) _, smallest = heapq.heappop(heap) return smallest # Test the function numbers = [10, 20, 4, 45, 99] print(find_smallest(numbers)) # Output: 4 # This code is contributed by Edula Vinay Kumar Reddy","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list import heapq def find_smallest(numbers): # Build a min heap using the elements in the list heap = [(x, x) for x in numbers] heapq.heapify(heap) # Return the root element (smallest element) _, smallest = heapq.heappop(heap) return smallest # Test the function numbers = [10, 20, 4, 45, 99] print(find_smallest(numbers)) # Output: 4 # This code is contributed by Edula Vinay Kumar Reddy #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"def Findsmall(itr, ele, list1): # recursive function to find smallest number if itr == len(list1): # base condition print(""The smallest number in the list is "", ele) return if list1[itr] < ele: # check the current element less than minimum or not ele = list1[itr] Findsmall(itr + 1, ele, list1) # recursive function call return # driver code lis = [5, 7, 2, 8, 9] ele = lis[0] Findsmall(0, ele, lis) # This code is contributed by Vinay Pinjala","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list def Findsmall(itr, ele, list1): # recursive function to find smallest number if itr == len(list1): # base condition print(""The smallest number in the list is "", ele) return if list1[itr] < ele: # check the current element less than minimum or not ele = list1[itr] Findsmall(itr + 1, ele, list1) # recursive function call return # driver code lis = [5, 7, 2, 8, 9] ele = lis[0] Findsmall(0, ele, lis) # This code is contributed by Vinay Pinjala #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# importing module import numpy as np # Initializing list lis = [5, 7, 2, 8, 9] # finding minimum value minimum = np.min(lis) # printing output print(""The smallest number in the list is"", minimum) # This code contributed by tvsk","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # importing module import numpy as np # Initializing list lis = [5, 7, 2, 8, 9] # finding minimum value minimum = np.min(lis) # printing output print(""The smallest number in the list is"", minimum) # This code contributed by tvsk #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"# defining the list arr = [5, 2, 3, 2, 5, 4, 7, 9, 7, 10, 15, 68] # converting the list into set set_arr = set(arr) # Now using the min function to get the minimum # value from the set print(min(set_arr))","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list # defining the list arr = [5, 2, 3, 2, 5, 4, 7, 9, 7, 10, 15, 68] # converting the list into set set_arr = set(arr) # Now using the min function to get the minimum # value from the set print(min(set_arr)) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find smallest number in a list,https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/,"arr = [2, 6, 8, 4, 9, 7, 52, 3, 6, 2, 4, 5, 6, 8, 2] min_val = min(arr) # Finding the minimum value values = {} # print item with position for pos, val in enumerate(arr): if val == min_val: values.update({pos: val}) # pos - Index of the smallest element # val - The value of the smallest element # get all min values print(values)","#Input : list1 = [10, 20, 4] #Output : 4","Python program to find smallest number in a list arr = [2, 6, 8, 4, 9, 7, 52, 3, 6, 2, 4, 5, 6, 8, 2] min_val = min(arr) # Finding the minimum value values = {} # print item with position for pos, val in enumerate(arr): if val == min_val: values.update({pos: val}) # pos - Index of the smallest element # val - The value of the smallest element # get all min values print(values) #Input : list1 = [10, 20, 4] #Output : 4 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Python program to find largest # number in a list # list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort() # printing the last element print(""Largest element is:"", list1[-1])","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Python program to find largest # number in a list # list of numbers list1 = [10, 20, 4, 45, 99] # sorting the list list1.sort() # printing the last element print(""Largest element is:"", list1[-1]) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Python program to find largest # number in a list # List of numbers list1 = [10, 20, 4, 45, 99] # Printing the maximum element print(""Largest element is:"", max(list1))","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Python program to find largest # number in a list # List of numbers list1 = [10, 20, 4, 45, 99] # Printing the maximum element print(""Largest element is:"", max(list1)) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Python program to find largest # number in a list # Creating an empty list list1 = [] # asking number of elements to put in list num = int(input(""Enter number of elements in list: "")) # Iterating till num to append elements in list for i in range(1, num + 1): ele = int(input(""Enter elements: "")) list1.append(ele) # Printing maximum element print(""Largest element is:"", max(list1))","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Python program to find largest # number in a list # Creating an empty list list1 = [] # asking number of elements to put in list num = int(input(""Enter number of elements in list: "")) # Iterating till num to append elements in list for i in range(1, num + 1): ele = int(input(""Enter elements: "")) list1.append(ele) # Printing maximum element print(""Largest element is:"", max(list1)) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Python program to find largest # number in a list def myMax(list1): # Assume first number in list is largest # initially and assign it to variable ""max"" max = list1[0] # Now traverse through the list and compare # each number with ""max"" value. Whichever is # largest assign that value to ""max'. for x in list1: if x > max: max = x # after complete traversing the list # return the ""max"" value return max # Driver code list1 = [10, 20, 4, 45, 99] print(""Largest element is:"", myMax(list1))","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Python program to find largest # number in a list def myMax(list1): # Assume first number in list is largest # initially and assign it to variable ""max"" max = list1[0] # Now traverse through the list and compare # each number with ""max"" value. Whichever is # largest assign that value to ""max'. for x in list1: if x > max: max = x # after complete traversing the list # return the ""max"" value return max # Driver code list1 = [10, 20, 4, 45, 99] print(""Largest element is:"", myMax(list1)) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Python code to find the largest number # in a list def maxelement(lst): # displaying largest element # one line solution print(max(lst)) # driver code # input list lst = [20, 10, 20, 4, 100] # the above input can also be given as # lst = list(map(int, input().split())) # -> taking input from the user maxelement(lst)","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Python code to find the largest number # in a list def maxelement(lst): # displaying largest element # one line solution print(max(lst)) # driver code # input list lst = [20, 10, 20, 4, 100] # the above input can also be given as # lst = list(map(int, input().split())) # -> taking input from the user maxelement(lst) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# python code to print largest element in the list lst = [20, 10, 20, 4, 100] print(max(lst, key=lambda value: int(value)))","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # python code to print largest element in the list lst = [20, 10, 20, 4, 100] print(max(lst, key=lambda value: int(value))) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"from functools import reduce lst = [20, 10, 20, 4, 100] largest_elem = reduce(max, lst) print(largest_elem)","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list from functools import reduce lst = [20, 10, 20, 4, 100] largest_elem = reduce(max, lst) print(largest_elem) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"# Function to find the largest element in the list def FindLargest(itr, ele, list1): # Base condition if itr == len(list1): print(""Largest element in the list is: "", ele) return # Check max condition if ele < list1[itr]: ele = list1[itr] # Recursive solution FindLargest(itr + 1, ele, list1) return # input list list1 = [2, 1, 7, 9, 5, 4] FindLargest(0, list1[0], list1)","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list # Function to find the largest element in the list def FindLargest(itr, ele, list1): # Base condition if itr == len(list1): print(""Largest element in the list is: "", ele) return # Check max condition if ele < list1[itr]: ele = list1[itr] # Recursive solution FindLargest(itr + 1, ele, list1) return # input list list1 = [2, 1, 7, 9, 5, 4] FindLargest(0, list1[0], list1) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"import heapq # list of numbers list1 = [10, 20, 4, 45, 99] # finding the largest element using heapq.nlargest() largest_element = heapq.nlargest(1, list1)[0] # printing the largest element print(""Largest element is:"", largest_element)","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list import heapq # list of numbers list1 = [10, 20, 4, 45, 99] # finding the largest element using heapq.nlargest() largest_element = heapq.nlargest(1, list1)[0] # printing the largest element print(""Largest element is:"", largest_element) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/,"import numpy as np # given list list1 = [2, 7, 5, 64, 14] # converting list to numpy array arr = np.array(list1) # finding largest numbers using np.max() method num = arr.max() # printing largest number print(num)","#Input : list1 = [10, 20, 4] #Output : 20","Python program to find largest number in a list import numpy as np # given list list1 = [2, 7, 5, 64, 14] # converting list to numpy array arr = np.array(list1) # finding largest numbers using np.max() method num = arr.max() # printing largest number print(num) #Input : list1 = [10, 20, 4] #Output : 20 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"# Python program to find second largest # number in a list # list of numbers - length of # list should be at least 2 list1 = [10, 20, 4, 45, 99] mx = max(list1[0], list1[1]) secondmax = min(list1[0], list1[1]) n = len(list1) for i in range(2, n): if list1[i] > mx: secondmax = mx mx = list1[i] elif list1[i] > secondmax and mx != list1[i]: secondmax = list1[i] elif mx == secondmax and secondmax != list1[i]: secondmax = list1[i] print(""Second highest number is : "", str(secondmax))"," #Output : Second highest number is : 45","Python program to find second largest number in a list # Python program to find second largest # number in a list # list of numbers - length of # list should be at least 2 list1 = [10, 20, 4, 45, 99] mx = max(list1[0], list1[1]) secondmax = min(list1[0], list1[1]) n = len(list1) for i in range(2, n): if list1[i] > mx: secondmax = mx mx = list1[i] elif list1[i] > secondmax and mx != list1[i]: secondmax = list1[i] elif mx == secondmax and secondmax != list1[i]: secondmax = list1[i] print(""Second highest number is : "", str(secondmax)) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"# Python program to find largest number # in a list # List of numbers list1 = [10, 20, 20, 4, 45, 45, 45, 99, 99] # Removing duplicates from the list list2 = list(set(list1)) # Sorting the list list2.sort() # Printing the second last element print(""Second largest element is:"", list2[-2])"," #Output : Second highest number is : 45","Python program to find second largest number in a list # Python program to find largest number # in a list # List of numbers list1 = [10, 20, 20, 4, 45, 45, 45, 99, 99] # Removing duplicates from the list list2 = list(set(list1)) # Sorting the list list2.sort() # Printing the second last element print(""Second largest element is:"", list2[-2]) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"# Python program to find second largest number # in a list # List of numbers list1 = [10, 20, 4, 45, 99] # new_list is a set of list1 new_list = set(list1) # Removing the largest element from temp list new_list.remove(max(new_list)) # Elements in original list are not changed # print(list1) print(max(new_list))"," #Output : Second highest number is : 45","Python program to find second largest number in a list # Python program to find second largest number # in a list # List of numbers list1 = [10, 20, 4, 45, 99] # new_list is a set of list1 new_list = set(list1) # Removing the largest element from temp list new_list.remove(max(new_list)) # Elements in original list are not changed # print(list1) print(max(new_list)) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"# Python program to find second largest # number in a list # creating list of integer type list1 = [10, 20, 4, 45, 99] """""" # sort the list list1.sort() # print second maximum element print(""Second largest element is:"", list1[-2]) """""" # print second maximum element using sorted() method print(""Second largest element is:"", sorted(list1)[-2])"," #Output : Second highest number is : 45","Python program to find second largest number in a list # Python program to find second largest # number in a list # creating list of integer type list1 = [10, 20, 4, 45, 99] """""" # sort the list list1.sort() # print second maximum element print(""Second largest element is:"", list1[-2]) """""" # print second maximum element using sorted() method print(""Second largest element is:"", sorted(list1)[-2]) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"def findLargest(arr): secondLargest = 0 largest = min(arr) for i in range(len(arr)): if arr[i] > largest: secondLargest = largest largest = arr[i] else: secondLargest = max(secondLargest, arr[i]) # Returning second largest element return secondLargest # Calling above method over this array set print(findLargest([10, 20, 4, 45, 99]))"," #Output : Second highest number is : 45","Python program to find second largest number in a list def findLargest(arr): secondLargest = 0 largest = min(arr) for i in range(len(arr)): if arr[i] > largest: secondLargest = largest largest = arr[i] else: secondLargest = max(secondLargest, arr[i]) # Returning second largest element return secondLargest # Calling above method over this array set print(findLargest([10, 20, 4, 45, 99])) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"def secondmax(arr): sublist = [x for x in arr if x < max(arr)] return max(sublist) if __name__ == ""__main__"": arr = [10, 20, 4, 45, 99] print(secondmax(arr))"," #Output : Second highest number is : 45","Python program to find second largest number in a list def secondmax(arr): sublist = [x for x in arr if x < max(arr)] return max(sublist) if __name__ == ""__main__"": arr = [10, 20, 4, 45, 99] print(secondmax(arr)) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"# python code to print second largest element in list lst = [10, 20, 4, 45, 99] maximum1 = max(lst) maximum2 = max(lst, key=lambda x: min(lst) - 1 if (x == maximum1) else x) print(maximum2)"," #Output : Second highest number is : 45","Python program to find second largest number in a list # python code to print second largest element in list lst = [10, 20, 4, 45, 99] maximum1 = max(lst) maximum2 = max(lst, key=lambda x: min(lst) - 1 if (x == maximum1) else x) print(maximum2) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"lst = [10, 20, 4, 45, 99] m = max(lst) x = [a for i, a in enumerate(lst) if a < m] print(max(x))"," #Output : Second highest number is : 45","Python program to find second largest number in a list lst = [10, 20, 4, 45, 99] m = max(lst) x = [a for i, a in enumerate(lst) if a < m] print(max(x)) #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"import heapq def find_second_largest(numbers): # Build a max heap using the elements in the list heap = [(-x, x) for x in numbers] heapq.heapify(heap) # Remove the root element (largest element) heapq.heappop(heap) # The new root element is the second largest element _, second_largest = heapq.heappop(heap) return second_largest # Test the function numbers = [10, 20, 4, 45, 99] print(find_second_largest(numbers)) # Output: 45"," #Output : Second highest number is : 45","Python program to find second largest number in a list import heapq def find_second_largest(numbers): # Build a max heap using the elements in the list heap = [(-x, x) for x in numbers] heapq.heapify(heap) # Remove the root element (largest element) heapq.heappop(heap) # The new root element is the second largest element _, second_largest = heapq.heappop(heap) return second_largest # Test the function numbers = [10, 20, 4, 45, 99] print(find_second_largest(numbers)) # Output: 45 #Output : Second highest number is : 45 [END]" Python program to find second largest number in a list,https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/,"import numpy as np def find_second_largest(arr): # creating numpy array np_arr = np.array(arr) # getting sorted indices sorted_indices = np.argsort(np_arr) # finding the second last index from sorted indices second_last_index = sorted_indices[-2] # returning the element at the second last index from original array return np_arr[second_last_index] # example usage arr = [10, 20, 4, 45, 99] print(find_second_largest(arr)) # Output: 45"," #Output : Second highest number is : 45","Python program to find second largest number in a list import numpy as np def find_second_largest(arr): # creating numpy array np_arr = np.array(arr) # getting sorted indices sorted_indices = np.argsort(np_arr) # finding the second last index from sorted indices second_last_index = sorted_indices[-2] # returning the element at the second last index from original array return np_arr[second_last_index] # example usage arr = [10, 20, 4, 45, 99] print(find_second_largest(arr)) # Output: 45 #Output : Second highest number is : 45 [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print Even Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] # iterating each number in list for num in list1: # checking condition if num % 2 == 0: print(num, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print Even Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] # iterating each number in list for num in list1: # checking condition if num % 2 == 0: print(num, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print Even Numbers in a List # Initializing list and value list1 = [10, 24, 4, 45, 66, 93] num = 0 # Uing while loop while num < len(list1): # Cecking condition if list1[num] % 2 == 0: print(list1[num], end="" "") # increment num num += 1","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print Even Numbers in a List # Initializing list and value list1 = [10, 24, 4, 45, 66, 93] num = 0 # Uing while loop while num < len(list1): # Cecking condition if list1[num] % 2 == 0: print(list1[num], end="" "") # increment num num += 1 Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print even Numbers in a List # Initializing list list1 = [10, 21, 4, 45, 66, 93] # using list comprehension even_nos = [num for num in list1 if num % 2 == 0] print(""Even numbers in the list: "", even_nos)","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print even Numbers in a List # Initializing list list1 = [10, 21, 4, 45, 66, 93] # using list comprehension even_nos = [num for num in list1 if num % 2 == 0] print(""Even numbers in the list: "", even_nos) Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print Even Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] # we can also print even no's using lambda exp. even_nos = list(filter(lambda x: (x % 2 == 0), list1)) print(""Even numbers in the list: "", even_nos)","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print Even Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] # we can also print even no's using lambda exp. even_nos = list(filter(lambda x: (x % 2 == 0), list1)) print(""Even numbers in the list: "", even_nos) Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print # even numbers in a list using recursion def evennumbers(list, n=0): # base case if n == len(list): exit() if list[n] % 2 == 0: print(list[n], end="" "") # calling function recursively evennumbers(list, n + 1) # Initializing list list1 = [10, 21, 4, 45, 66, 93] print(""Even numbers in the list:"", end="" "") evennumbers(list1)","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print # even numbers in a list using recursion def evennumbers(list, n=0): # base case if n == len(list): exit() if list[n] % 2 == 0: print(list[n], end="" "") # calling function recursively evennumbers(list, n + 1) # Initializing list list1 = [10, 21, 4, 45, 66, 93] print(""Even numbers in the list:"", end="" "") evennumbers(list1) Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"list1 = [2, 7, 5, 64, 14] for a, i in enumerate(list1): if i % 2 == 0: print(i, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list list1 = [2, 7, 5, 64, 14] for a, i in enumerate(list1): if i % 2 == 0: print(i, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"list1 = [2, 7, 5, 64, 14] for i in list1: if i % 2 != 0: pass else: print(i, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list list1 = [2, 7, 5, 64, 14] for i in list1: if i % 2 != 0: pass else: print(i, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python code To print all even numbers # in a given list using numpy array import numpy as np # Declaring Range temp = [2, 7, 5, 64, 14] li = np.array(temp) # printing odd numbers using numpy array even_num = li[li % 2 == 0] print(even_num)","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python code To print all even numbers # in a given list using numpy array import numpy as np # Declaring Range temp = [2, 7, 5, 64, 14] li = np.array(temp) # printing odd numbers using numpy array even_num = li[li % 2 == 0] print(even_num) Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# python program to print all even no's in a list # defining list with even and odd numbers list1 = [39, 28, 19, 45, 33, 74, 56] # traversing list using for loop for element in list1: if not element & 1: # condition to check even or not print(element, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # python program to print all even no's in a list # defining list with even and odd numbers list1 = [39, 28, 19, 45, 33, 74, 56] # traversing list using for loop for element in list1: if not element & 1: # condition to check even or not print(element, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Python program to print all even no's in a list # Defining list with even and odd numbers # Initializing list list1 = [39, 28, 19, 45, 33, 74, 56] # Traversing list using for loop for element in list1: # condition to check even or not if element | 1 != element: print(element, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Python program to print all even no's in a list # Defining list with even and odd numbers # Initializing list list1 = [39, 28, 19, 45, 33, 74, 56] # Traversing list using for loop for element in list1: # condition to check even or not if element | 1 != element: print(element, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Using the reduce function from the functools module from functools import reduce list1 = [39, 28, 19, 45, 33, 74, 56] even_numbers = reduce(lambda x, y: x + [y] if y % 2 == 0 else x, list1, []) for num in even_numbers: print(num, end="" "") # This code is contributed by Jyothi pinjala","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Using the reduce function from the functools module from functools import reduce list1 = [39, 28, 19, 45, 33, 74, 56] even_numbers = reduce(lambda x, y: x + [y] if y % 2 == 0 else x, list1, []) for num in even_numbers: print(num, end="" "") # This code is contributed by Jyothi pinjala Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"import numpy as np # given list list1 = [2, 7, 5, 64, 14] # converting list to numpy array arr = np.array(list1) # finding even numbers using where() method even_num = arr[np.where(arr % 2 == 0)] # printing even numbers print(even_num)","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list import numpy as np # given list list1 = [2, 7, 5, 64, 14] # converting list to numpy array arr = np.array(list1) # finding even numbers using where() method even_num = arr[np.where(arr % 2 == 0)] # printing even numbers print(even_num) Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print even numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/,"# Using the filterfalse function from the itertools module from itertools import filterfalse # Test list1 list1 = [39, 28, 19, 45, 33, 74, 56] # filtering even number even_numbers = filterfalse(lambda y: y % 2, list1) # Printing result for num in even_numbers: print(num, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]","Python program to print even numbers in a list # Using the filterfalse function from the itertools module from itertools import filterfalse # Test list1 list1 = [39, 28, 19, 45, 33, 74, 56] # filtering even number even_numbers = filterfalse(lambda y: y % 2, list1) # Printing result for num in even_numbers: print(num, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] # iterating each number in list for num in list1: # checking condition if num % 2 != 0: print(num, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] # iterating each number in list for num in list1: # checking condition if num % 2 != 0: print(num, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] i = 0 # using while loop while i < len(list1): # checking condition if list1[i] % 2 != 0: print(list1[i], end="" "") # increment i i += 1","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] i = 0 # using while loop while i < len(list1): # checking condition if list1[i] % 2 != 0: print(list1[i], end="" "") # increment i i += 1 Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] only_odd = [num for num in list1 if num % 2 == 1] print(only_odd)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93] only_odd = [num for num in list1 if num % 2 == 1] print(only_odd) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] # we can also print odd no's using lambda exp. odd_nos = list(filter(lambda x: (x % 2 != 0), list1)) print(""Odd numbers in the list:"", odd_nos)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] # we can also print odd no's using lambda exp. odd_nos = list(filter(lambda x: (x % 2 != 0), list1)) print(""Odd numbers in the list:"", odd_nos) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd numbers in a List lst = [10, 21, 4, 45, 66, 93, 11] for i in lst: if i % 2 == 0: pass else: print(i, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd numbers in a List lst = [10, 21, 4, 45, 66, 93, 11] for i in lst: if i % 2 == 0: pass else: print(i, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print # odd numbers in a list using recursion def oddnumbers(list, n=0): # base case if n == len(list): exit() if list[n] % 2 != 0: print(list[n], end="" "") # calling function recursively oddnumbers(list, n + 1) list1 = [10, 21, 4, 45, 66, 93, 11] print(""odd numbers in the list:"", end="" "") oddnumbers(list1)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print # odd numbers in a list using recursion def oddnumbers(list, n=0): # base case if n == len(list): exit() if list[n] % 2 != 0: print(list[n], end="" "") # calling function recursively oddnumbers(list, n + 1) list1 = [10, 21, 4, 45, 66, 93, 11] print(""odd numbers in the list:"", end="" "") oddnumbers(list1) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"list1 = [2, 7, 5, 64, 14] for a, i in enumerate(list1): if i % 2 != 0: print(i, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List list1 = [2, 7, 5, 64, 14] for a, i in enumerate(list1): if i % 2 != 0: print(i, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List import numpy as np # list of numbers list1 = np.array([10, 21, 4, 45, 66, 93]) only_odd = list1[list1 % 2 == 1] print(only_odd)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd Numbers in a List import numpy as np # list of numbers list1 = np.array([10, 21, 4, 45, 66, 93]) only_odd = list1[list1 % 2 == 1] print(only_odd) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# List of numbers list1 = [9, 5, 4, 7, 2] for ele in list1: if ele & 1: # Checking the element odd or not print(ele, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # List of numbers list1 = [9, 5, 4, 7, 2] for ele in list1: if ele & 1: # Checking the element odd or not print(ele, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# List of numbers list1 = [9, 5, 4, 7, 2] for ele in list1: if ele | 1 == ele: # Checking the element odd or not print(ele, end="" "")","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # List of numbers list1 = [9, 5, 4, 7, 2] for ele in list1: if ele | 1 == ele: # Checking the element odd or not print(ele, end="" "") Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"def is_odd(number): return number % 2 == 1 def print_odd_numbers(numbers): odd_numbers = list(filter(is_odd, numbers)) return odd_numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(print_odd_numbers(numbers))","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List def is_odd(number): return number % 2 == 1 def print_odd_numbers(numbers): odd_numbers = list(filter(is_odd, numbers)) return odd_numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(print_odd_numbers(numbers)) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"import numpy as np # list of numbers list1 = [10, 21, 4, 45, 66, 93] # convert list to numpy array arr = np.array(list1) # find indices where elements are odd idx = np.where(arr % 2 != 0) # extract elements at odd indices only_odd = arr[idx] # print only odd elements print(only_odd)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List import numpy as np # list of numbers list1 = [10, 21, 4, 45, 66, 93] # convert list to numpy array arr = np.array(list1) # find indices where elements are odd idx = np.where(arr % 2 != 0) # extract elements at odd indices only_odd = arr[idx] # print only odd elements print(only_odd) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List from functools import reduce # list of numbers list1 = [10, 21, 4, 45, 66, 93] # Using reduce method in list odd_list = reduce(lambda a, b: a + [b] if b % 2 else a, list1, []) print(odd_list)","Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5]","Python program to print odd numbers in a List # Python program to print odd Numbers in a List from functools import reduce # list of numbers list1 = [10, 21, 4, 45, 66, 93] # Using reduce method in list odd_list = reduce(lambda a, b: a + [b] if b % 2 else a, list1, []) print(odd_list) Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python program to print all even numbers in range for even_numbers in range(4, 15, 2): # here inside range function first no denotes starting, # second denotes end and # third denotes the interval print(even_numbers, end="" "")","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python program to print all even numbers in range for even_numbers in range(4, 15, 2): # here inside range function first no denotes starting, # second denotes end and # third denotes the interval print(even_numbers, end="" "") Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 == 0: print(num, end="" "")","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 == 0: print(num, end="" "") Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # creating even starting range start = start + 1 if start & 1 else start # create a list and printing element # contains Even numbers in range [print(x) for x in range(start, end + 1, 2)]","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # creating even starting range start = start + 1 if start & 1 else start # create a list and printing element # contains Even numbers in range [print(x) for x in range(start, end + 1, 2)] Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"def even(num1, num2): if num1 > num2: return print(num1, end="" "") return even(num1 + 2, num2) num1 = 4 num2 = 15 even(num1, num2)","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range def even(num1, num2): if num1 > num2: return print(num1, end="" "") return even(num1 + 2, num2) num1 = 4 num2 = 15 even(num1, num2) Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python code To print all even numbers # in a given range using the lambda function a = 4 b = 15 li = [] for i in range(a, b + 1): li.append(i) # printing odd numbers using the lambda function even_num = list(filter(lambda x: (x % 2 == 0), li)) print(even_num)","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python code To print all even numbers # in a given range using the lambda function a = 4 b = 15 li = [] for i in range(a, b + 1): li.append(i) # printing odd numbers using the lambda function even_num = list(filter(lambda x: (x % 2 == 0), li)) print(even_num) Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"x = [i for i in range(4, 15 + 1) if i % 2 == 0] print(*x)","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range x = [i for i in range(4, 15 + 1) if i % 2 == 0] print(*x) Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"a = 4 b = 15 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a % 2 == 0])","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range a = 4 b = 15 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a % 2 == 0]) Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"a = 4 b = 15 for i in range(a, b + 1): if i % 2 != 0: pass else: print(i, end="" "")","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range a = 4 b = 15 for i in range(a, b + 1): if i % 2 != 0: pass else: print(i, end="" "") Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python code To print all even numbers # in a given range using numpy array import numpy as np # Declaring Range a = 4 b = 15 li = np.array(range(a, b + 1)) # printing odd numbers using numpy array even_num = li[li % 2 == 0] print(even_num)","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python code To print all even numbers # in a given range using numpy array import numpy as np # Declaring Range a = 4 b = 15 li = np.array(range(a, b + 1)) # printing odd numbers using numpy array even_num = li[li % 2 == 0] print(even_num) Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python program to print even Numbers in given range # using bitwise & operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if not (num & 1): print(num, end="" "") # this code is contributed by vinay pinjala.","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python program to print even Numbers in given range # using bitwise & operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if not (num & 1): print(num, end="" "") # this code is contributed by vinay pinjala. Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all even numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/,"# Python program to print even Numbers in given range # using bitwise | operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if not (num | 1) == num: print(num, end="" "") # this code is contributed by tvsk","Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14","Python program to print all even numbers in a range # Python program to print even Numbers in given range # using bitwise | operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if not (num | 1) == num: print(num, end="" "") # this code is contributed by tvsk Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print odd Numbers in given range start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 != 0: print(num, end="" "")","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print odd Numbers in given range start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 != 0: print(num, end="" "") Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print Even Numbers in given range start = int(input(""Enter the start of range:"")) end = int(input(""Enter the end of range:"")) # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 != 0: print(num)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print Even Numbers in given range start = int(input(""Enter the start of range:"")) end = int(input(""Enter the end of range:"")) # iterating each number in list for num in range(start, end + 1): # checking condition if num % 2 != 0: print(num) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Uncomment the below two lines for taking the User Inputs # start = int(input(""Enter the start of range: "")) # end = int(input(""Enter the end of range: "")) # Range declaration start = 5 end = 20 if start % 2 != 0: for num in range(start, end + 1, 2): print(num, end="" "") else: for num in range(start + 1, end + 1, 2): print(num, end="" "")","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Uncomment the below two lines for taking the User Inputs # start = int(input(""Enter the start of range: "")) # end = int(input(""Enter the end of range: "")) # Range declaration start = 5 end = 20 if start % 2 != 0: for num in range(start, end + 1, 2): print(num, end="" "") else: for num in range(start + 1, end + 1, 2): print(num, end="" "") Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # create a list that contains only Even numbers in given range even_list = range(start, end + 1)[start % 2 :: 2] for num in even_list: print(num, end="" "")","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print Even Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # create a list that contains only Even numbers in given range even_list = range(start, end + 1)[start % 2 :: 2] for num in even_list: print(num, end="" "") Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python code To print all odd numbers # in a given range using the lambda function a = 3 b = 11 li = [] for i in range(a, b + 1): li.append(i) # printing odd numbers using the lambda function odd_num = list(filter(lambda x: (x % 2 != 0), li)) print(odd_num)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python code To print all odd numbers # in a given range using the lambda function a = 3 b = 11 li = [] for i in range(a, b + 1): li.append(i) # printing odd numbers using the lambda function odd_num = list(filter(lambda x: (x % 2 != 0), li)) print(odd_num) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"def odd(num1, num2): if num1 > num2: return if num1 & 1: print(num1, end="" "") return odd(num1 + 2, num2) else: return odd(num1 + 1, num2) num1 = 5 num2 = 15 odd(num1, num2)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range def odd(num1, num2): if num1 > num2: return if num1 & 1: print(num1, end="" "") return odd(num1 + 2, num2) else: return odd(num1 + 1, num2) num1 = 5 num2 = 15 odd(num1, num2) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"x = [i for i in range(4, 15 + 1) if i % 2 != 0] print(*x)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range x = [i for i in range(4, 15 + 1) if i % 2 != 0] print(*x) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"a = 4 b = 15 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a % 2 != 0])","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range a = 4 b = 15 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a % 2 != 0]) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"a = 4 b = 15 for i in range(a, b + 1): if i % 2 == 0: pass else: print(i, end="" "")","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range a = 4 b = 15 for i in range(a, b + 1): if i % 2 == 0: pass else: print(i, end="" "") Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print Even Numbers in given range # Range declaration a = 4 b = 15 # create a list that contains only Even numbers in given range l = filter(lambda a: a % 2, range(a, b + 1)) print(*l)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print Even Numbers in given range # Range declaration a = 4 b = 15 # create a list that contains only Even numbers in given range l = filter(lambda a: a % 2, range(a, b + 1)) print(*l) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print odd Numbers in given range # using bitwise & operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num & 1 != 0: print(num, end="" "") # this code is contributed by vinay pinjala.","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print odd Numbers in given range # using bitwise & operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num & 1 != 0: print(num, end="" "") # this code is contributed by vinay pinjala. Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python program to print odd Numbers in given range # using bitwise | operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num | 1 == num: print(num, end="" "")","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python program to print odd Numbers in given range # using bitwise | operator start, end = 4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num | 1 == num: print(num, end="" "") Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"import numpy as np # Range declaration a = 3 b = 15 # Create an array containing numbers in the range arr = np.arange(a, b + 1) # Create a new array containing only even numbers evens = arr[arr % 2 != 0] # Print the array of even numbers print(*evens) # This code is contributed by Jyothi pinjala","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range import numpy as np # Range declaration a = 3 b = 15 # Create an array containing numbers in the range arr = np.arange(a, b + 1) # Create a new array containing only even numbers evens = arr[arr % 2 != 0] # Print the array of even numbers print(*evens) # This code is contributed by Jyothi pinjala Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"a = 4 b = 15 for i in range(a, b + 1): if i % 2 == 0: continue else: print(i, end="" "") # This Code is contributed by Pratik Gupta (guptapratik)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range a = 4 b = 15 for i in range(a, b + 1): if i % 2 == 0: continue else: print(i, end="" "") # This Code is contributed by Pratik Gupta (guptapratik) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to print all odd numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/,"# Python 3 code to demonstrate # print odd number in range import itertools # Range declaration a = 3 b = 15 # using filterfalse function evens = list(itertools.filterfalse(lambda x: x % 2 == 0, range(a, b + 1))) # Print the array of even numbers print(*evens)","Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15","Python program to print all odd numbers in a range # Python 3 code to demonstrate # print odd number in range import itertools # Range declaration a = 3 b = 15 # using filterfalse function evens = list(itertools.filterfalse(lambda x: x % 2 == 0, range(a, b + 1))) # Print the array of even numbers print(*evens) Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 # iterating each number in list for num in list1: # checking condition if num % 2 == 0: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 # iterating each number in list for num in list1: # checking condition if num % 2 == 0: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] even_count, odd_count = 0, 0 num = 0 # using while loop while num < len(list1): # checking condition if list1[num] % 2 == 0: even_count += 1 else: odd_count += 1 # increment num num += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] even_count, odd_count = 0, 0 num = 0 # using while loop while num < len(list1): # checking condition if list1[num] % 2 == 0: even_count += 1 else: odd_count += 1 # increment num num += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] odd_count = len(list(filter(lambda x: (x % 2 != 0), list1))) # we can also do len(list1) - odd_count even_count = len(list(filter(lambda x: (x % 2 == 0), list1))) print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] odd_count = len(list(filter(lambda x: (x % 2 != 0), list1))) # we can also do len(list1) - odd_count even_count = len(list(filter(lambda x: (x % 2 == 0), list1))) print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] only_odd = [num for num in list1 if num % 2 == 1] odd_count = len(only_odd) print(""Even numbers in the list: "", len(list1) - odd_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] only_odd = [num for num in list1 if num % 2 == 1] odd_count = len(only_odd) print(""Even numbers in the list: "", len(list1) - odd_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # using recursion even_count = 0 # even counter i = 0 # index, so that we can check if list[i] is even or odd odd_count = 0 # odd counter def evenoddcount(lst): # defining local counters as global variable global even_count global odd_count global i if lst[i] % 2 == 0: # check if number is even even_count += 1 else: # if number is odd odd_count += 1 if i in range(len(lst) - 1): i += 1 # increment i evenoddcount(lst) # calling fonction recursively else: print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) list1 = [10, 21, 4, 45, 66, 93, 1] evenoddcount(list1)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # using recursion even_count = 0 # even counter i = 0 # index, so that we can check if list[i] is even or odd odd_count = 0 # odd counter def evenoddcount(lst): # defining local counters as global variable global even_count global odd_count global i if lst[i] % 2 == 0: # check if number is even even_count += 1 else: # if number is odd odd_count += 1 if i in range(len(lst) - 1): i += 1 # increment i evenoddcount(lst) # calling fonction recursively else: print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) list1 = [10, 21, 4, 45, 66, 93, 1] evenoddcount(list1) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # using Bitwise XOR # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if num ^ 1 == num + 1: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) # This code is contributed by Shivesh Kumar Dwivedi"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # using Bitwise XOR # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if num ^ 1 == num + 1: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) # This code is contributed by Shivesh Kumar Dwivedi #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # using Bitwise AND # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if not num & 1: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # using Bitwise AND # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if not num & 1: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # using Bitwise OR # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if num | 1 > num: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) # This code is contributed by Shivesh Kumar Dwivedi"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # using Bitwise OR # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 for num in list1: # checking condition if num | 1 > num: even_count += 1 else: odd_count += 1 print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) # This code is contributed by Shivesh Kumar Dwivedi #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"lst = [12, 14, 95, 3] c = 0 c1 = 0 for i, a in enumerate(lst): if a % 2 == 0: c += 1 else: c1 += 1 print(""even number count"", c, ""odd number count"", c1)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List lst = [12, 14, 95, 3] c = 0 c1 = 0 for i, a in enumerate(lst): if a % 2 == 0: c += 1 else: c1 += 1 print(""even number count"", c, ""odd number count"", c1) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to Print Positive Numbers in a List import numpy as np # list of numbers List = [10, 21, 4, 45, 66, 93, 11] # using numpy Array list1 = np.array(List) Even_list = list1[list1 % 2 == 0] print(""Even numbers in the list: "", len(Even_list)) print(""Odd numbers in the list: "", len(list1) - len(Even_list))"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to Print Positive Numbers in a List import numpy as np # list of numbers List = [10, 21, 4, 45, 66, 93, 11] # using numpy Array list1 = np.array(List) Even_list = list1[list1 % 2 == 0] print(""Even numbers in the list: "", len(Even_list)) print(""Odd numbers in the list: "", len(list1) - len(Even_list)) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"import numpy as np # list of numbers list1 = [2, 7, 5, 64, 14] # create numpy array from list arr = np.array(list1) # count even numbers even_count = len(np.where(arr % 2 == 0)[0]) # count odd numbers odd_count = len(np.where(arr % 2 == 1)[0]) # print counts print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List import numpy as np # list of numbers list1 = [2, 7, 5, 64, 14] # create numpy array from list arr = np.array(list1) # count even numbers even_count = len(np.where(arr % 2 == 0)[0]) # count odd numbers odd_count = len(np.where(arr % 2 == 1)[0]) # print counts print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to count Even and Odd numbers in a List,https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/,"# Python program to count Even # and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 # Using sum function odd_count = sum(1 for i in list1 if i & 1) even_count = len(list1) - odd_count print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count)"," #Output : Even numbers in the list: 3","Python program to count Even and Odd numbers in a List # Python program to count Even # and Odd numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 1] even_count, odd_count = 0, 0 # Using sum function odd_count = sum(1 for i in list1 if i & 1) even_count = len(list1) - odd_count print(""Even numbers in the list: "", even_count) print(""Odd numbers in the list: "", odd_count) #Output : Even numbers in the list: 3 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print positive Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition????????????????????????if num & gt????????????????????????= 0:????????????????????????????????????????","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print positive Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition????????????????????????if num & gt????????????????????????= 0:???????????????????????????????????????? Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, -4, -45, -66, 93] num = 0 # using while loop while num < len(list1): # checking condition if list1[num] >= 0: print(list1[num], end="" "") # increment num num += 1","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, -4, -45, -66, 93] num = 0 # using while loop while num < len(list1): # checking condition if list1[num] >= 0: print(list1[num], end="" "") # increment num num += 1 Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print Positive Numbers in a List # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using list comprehension pos_nos = [num for num in list1 if num >= 0] print(""Positive numbers in the list: "", *pos_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print Positive Numbers in a List # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using list comprehension pos_nos = [num for num in list1 if num >= 0] print(""Positive numbers in the list: "", *pos_nos) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # we can also print positive no's using lambda exp. pos_nos = list(filter(lambda x: (x >= 0), list1)) print(""Positive numbers in the list: "", *pos_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # we can also print positive no's using lambda exp. pos_nos = list(filter(lambda x: (x >= 0), list1)) print(""Positive numbers in the list: "", *pos_nos) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"l = [12, -7, 5, 64, -14] print([a for j, a in enumerate(l) if a >= 0])","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list l = [12, -7, 5, 64, -14] print([a for j, a in enumerate(l) if a >= 0]) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print positive numbers in a List # list of numbers list1 = [11, -21, 0, 45, 66, -93] res = [] list2 = list(map(str, list1)) for i in range(0, len(list2)): if not list2[i].startswith(""-"") and list2[i] != ""0"": res.append(str(list1[i])) res = "" "".join(res) print(res)","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print positive numbers in a List # list of numbers list1 = [11, -21, 0, 45, 66, -93] res = [] list2 = list(map(str, list1)) for i in range(0, len(list2)): if not list2[i].startswith(""-"") and list2[i] != ""0"": res.append(str(list1[i])) res = "" "".join(res) print(res) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print Positive Numbers in a List import numpy as np # list of numbers list1 = np.array([-10, -21, -4, 45, -66, 93]) # using numpy Array pos_nos = list1[list1 >= 0] print(""Positive numbers in the list: "", *pos_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print Positive Numbers in a List import numpy as np # list of numbers list1 = np.array([-10, -21, -4, 45, -66, 93]) # using numpy Array pos_nos = list1[list1 >= 0] print(""Positive numbers in the list: "", *pos_nos) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Function to print even numbers in a list def PrintEven(itr, list1): if itr == len(list1): # Base Condition return if list1[itr] >= 0: print(list1[itr], end="" "") PrintEven(itr + 1, list1) # Recursive Function Call return list1 = [-5, 7, -19, 10, 9] # list of numbers PrintEven(0, list1) # Function Call # This code is contributed by vinay pinjala","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Function to print even numbers in a list def PrintEven(itr, list1): if itr == len(list1): # Base Condition return if list1[itr] >= 0: print(list1[itr], end="" "") PrintEven(itr + 1, list1) # Recursive Function Call return list1 = [-5, 7, -19, 10, 9] # list of numbers PrintEven(0, list1) # Function Call # This code is contributed by vinay pinjala Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print positive numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/,"# Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] import operator pos_nos = [] for i in list1: if operator.ge(i, 0): pos_nos.append(i) print(""Positive numbers in the list: "", pos_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64","Python program to print positive numbers in a list # Python program to print positive Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] import operator pos_nos = [] for i in list1: if operator.ge(i, 0): pos_nos.append(i) print(""Positive numbers in the list: "", pos_nos) Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition??????????????"" "")","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition??????????????"" "") Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List # list of numbers list1 = [-10, 21, -4, -45, -66, 93] num = 0 # using while loop while num < len(list1): # checking condition if list1[num] < 0: print(list1[num], end="" "") # increment num num += 1","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List # list of numbers list1 = [-10, 21, -4, -45, -66, 93] num = 0 # using while loop while num < len(list1): # checking condition if list1[num] < 0: print(list1[num], end="" "") # increment num num += 1 Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using list comprehension neg_nos = [num for num in list1 if num < 0] print(""Negative numbers in the list: "", *neg_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using list comprehension neg_nos = [num for num in list1 if num < 0] print(""Negative numbers in the list: "", *neg_nos) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # we can also print negative no's using lambda exp. neg_nos = list(filter(lambda x: (x < 0), list1)) print(""Negative numbers in the list: "", *neg_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # we can also print negative no's using lambda exp. neg_nos = list(filter(lambda x: (x < 0), list1)) print(""Negative numbers in the list: "", *neg_nos) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"l = [12, -7, 5, 64, -14] print([a for j, a in enumerate(l) if a < 0])","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list l = [12, -7, 5, 64, -14] print([a for j, a in enumerate(l) if a < 0]) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List # list of numbers list1 = [11, -21, 0, 45, 66, -93] res = [] list2 = list(map(str, list1)) for i in range(0, len(list2)): if list2[i].startswith(""-""): res.append(str(list1[i])) res = "" "".join(res) print(res)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List # list of numbers list1 = [11, -21, 0, 45, 66, -93] res = [] list2 = list(map(str, list1)) for i in range(0, len(list2)): if list2[i].startswith(""-""): res.append(str(list1[i])) res = "" "".join(res) print(res) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List import numpy as np # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # Using numpy to print the negative number temp = np.array(list1) neg_nos = temp[temp <= 0] print(""Negative numbers in the list: "", *neg_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List import numpy as np # list of numbers list1 = [-10, 21, 4, -45, -66, 93, -11] # Using numpy to print the negative number temp = np.array(list1) neg_nos = temp[temp <= 0] print(""Negative numbers in the list: "", *neg_nos) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Recursive function to check current element negative or not def PrintNegative(itr, list1): if itr == len(list1): # base condition return if list1[itr] < 0: # check negative number or not print(list1[itr], end="" "") PrintNegative(itr + 1, list1) # recursive function call list1 = [-1, 8, 9, -5, 7] PrintNegative(0, list1)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Recursive function to check current element negative or not def PrintNegative(itr, list1): if itr == len(list1): # base condition return if list1[itr] < 0: # check negative number or not print(list1[itr], end="" "") PrintNegative(itr + 1, list1) # recursive function call list1 = [-1, 8, 9, -5, 7] PrintNegative(0, list1) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"import numpy as np # list of numbers list1 = [12, -7, 5, 64, -14] # converting list to numpy array arr1 = np.array(list1) # finding negative numbers in the array neg_nums = arr1[np.where(arr1 < 0)] # printing negative numbers print(""Negative numbers in the list: "", *neg_nums)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list import numpy as np # list of numbers list1 = [12, -7, 5, 64, -14] # converting list to numpy array arr1 = np.array(list1) # finding negative numbers in the array neg_nums = arr1[np.where(arr1 < 0)] # printing negative numbers print(""Negative numbers in the list: "", *neg_nums) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/,"# Python program to print negative Numbers in a List from functools import reduce # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using reduce method neg_nos = reduce(lambda a, b: a + [b] if b < 0 else a, list1, []) print(""Negative numbers in the list: "", *neg_nos)","Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14","Python program to print negative numbers in a list # Python program to print negative Numbers in a List from functools import reduce # list of numbers list1 = [-10, -21, -4, 45, -66, 93] # using reduce method neg_nos = reduce(lambda a, b: a + [b] if b < 0 else a, list1, []) print(""Negative numbers in the list: "", *neg_nos) Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"# Python program to print positive Numbers in given range start, end = -4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num >= 0: print(num, end="" "")","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range # Python program to print positive Numbers in given range start, end = -4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num >= 0: print(num, end="" "") Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"# Python program to print positive Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num >= 0: print(num, end="" "")","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range # Python program to print positive Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num >= 0: print(num, end="" "") Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"# Python code To print all positive numbers # in a given range using the lambda function a = -4 b = 5 li = [] for i in range(a, b + 1): li.append(i) # printing positive numbers using the lambda function positive_num = list(filter(lambda x: (x >= 0), li)) print(positive_num)","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range # Python code To print all positive numbers # in a given range using the lambda function a = -4 b = 5 li = [] for i in range(a, b + 1): li.append(i) # printing positive numbers using the lambda function positive_num = list(filter(lambda x: (x >= 0), li)) print(positive_num) Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"# Python code # To print all positive numbers in a given range a = -4 b = 5 out = [i for i in range(a, b + 1) if i > 0] # print the all positive numbers print(*out)","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range # Python code # To print all positive numbers in a given range a = -4 b = 5 out = [i for i in range(a, b + 1) if i > 0] # print the all positive numbers print(*out) Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"a = -4 b = 5 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a >= 0])","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range a = -4 b = 5 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a >= 0]) Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"a = -4 b = 5 for i in range(a, b + 1): if i < 0: pass else: print(i, end="" "")","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range a = -4 b = 5 for i in range(a, b + 1): if i < 0: pass else: print(i, end="" "") Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"def printPositives(start, end): # defining recursive function to print positives if start == end: return # base condition if start >= 0: # check for positive number print(start, end="" "") printPositives(start + 1, end) # recursive calling a, b = -5, 10 printPositives(a, b) # function calling","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range def printPositives(start, end): # defining recursive function to print positives if start == end: return # base condition if start >= 0: # check for positive number print(start, end="" "") printPositives(start + 1, end) # recursive calling a, b = -5, 10 printPositives(a, b) # function calling Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all positive numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/,"a = -4 b = 5 positive_nums = list(filter(lambda x: x >= 0, range(a, b + 1))) print(positive_nums)","Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 ","Python program to print all positive numbers in a range a = -4 b = 5 positive_nums = list(filter(lambda x: x >= 0, range(a, b + 1))) print(positive_nums) Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"# Python code # To print all negative numbers in a given range def negativenumbers(a, b): # Checking condition for negative numbers # single line solution out = [i for i in range(a, b + 1) if i < 0] # print the all negative numbers print(*out) # driver code # a -> start range a = -4 # b -> end range b = 5 negativenumbers(a, b)","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range # Python code # To print all negative numbers in a given range def negativenumbers(a, b): # Checking condition for negative numbers # single line solution out = [i for i in range(a, b + 1) if i < 0] # print the all negative numbers print(*out) # driver code # a -> start range a = -4 # b -> end range b = 5 negativenumbers(a, b) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"# Python program to print negative Numbers in given range start, end = -4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num < 0: print(num, end="" "")","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range # Python program to print negative Numbers in given range start, end = -4, 19 # iterating each number in list for num in range(start, end + 1): # checking condition if num < 0: print(num, end="" "") Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"# Python program to print negative Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num < 0: print(num, end="" "")","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range # Python program to print negative Numbers in given range start = int(input(""Enter the start of range: "")) end = int(input(""Enter the end of range: "")) # iterating each number in list for num in range(start, end + 1): # checking condition if num < 0: print(num, end="" "") Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"# Python code To print all negative # numbers in a given range using lambda function # inputs a = -4 b = 5 li = [] for i in range(a, b): li.append(i) # printing negative numbers using the lambda function negative_num = list(filter(lambda x: (x < 0), li)) print(negative_num)","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range # Python code To print all negative # numbers in a given range using lambda function # inputs a = -4 b = 5 li = [] for i in range(a, b): li.append(i) # printing negative numbers using the lambda function negative_num = list(filter(lambda x: (x < 0), li)) print(negative_num) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"a = -4 b = 5 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a < 0])","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range a = -4 b = 5 l = [] for i in range(a, b + 1): l.append(i) print([a for j, a in enumerate(l) if a < 0]) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"a = -4 b = 5 print([i for i in range(a, b + 1) if i < 0])","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range a = -4 b = 5 print([i for i in range(a, b + 1) if i < 0]) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"a = -4 b = 5 for i in range(a, b + 1): if i >= 0: pass else: print(i, end="" "")","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range a = -4 b = 5 for i in range(a, b + 1): if i >= 0: pass else: print(i, end="" "") Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"# Recursive function to print Negative numbers def PrintNegative(itr, end): if itr == end: # Base Condition return if itr < 0: # checking Negative or not print(itr, end="" "") PrintNegative(itr + 1, end) # Recursive function call return a = -5 b = 5 PrintNegative(a, b)","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range # Recursive function to print Negative numbers def PrintNegative(itr, end): if itr == end: # Base Condition return if itr < 0: # checking Negative or not print(itr, end="" "") PrintNegative(itr + 1, end) # Recursive function call return a = -5 b = 5 PrintNegative(a, b) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to print all negative numbers in a range,https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/,"import numpy as np # Taking input values for start and end of the range start = -4 end = 5 # Creating an array using numpy.arange() arr = np.arange(start, end + 1) # Filtering negative numbers using numpy.where() neg_arr = arr[np.where(arr < 0)] # Printing the resulting array print(neg_arr)","Input: a = -4, b = 5 Output: -4, -3, -2, -1","Python program to print all negative numbers in a range import numpy as np # Taking input values for start and end of the range start = -4 end = 5 # Creating an array using numpy.arange() arr = np.arange(start, end + 1) # Filtering negative numbers using numpy.where() neg_arr = arr[np.where(arr < 0)] # Printing the resulting array print(neg_arr) Input: a = -4, b = 5 Output: -4, -3, -2, -1 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1] pos_count, neg_count = 0, 0 # iterating each number in list for num in list1: # checking condition if num >= 0: pos_count += 1 else: neg_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1] pos_count, neg_count = 0, 0 # iterating each number in list for num in list1: # checking condition if num >= 0: pos_count += 1 else: neg_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, 93, 11] pos_count, neg_count = 0, 0 num = 0 # using while loop while num < len(list1): # checking condition if list1[num] >= 0: pos_count += 1 else: neg_count += 1 # increment num num += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, 93, 11] pos_count, neg_count = 0, 0 num = 0 # using while loop while num < len(list1): # checking condition if list1[num] >= 0: pos_count += 1 else: neg_count += 1 # increment num num += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive # and negative numbers in a List # list of numbers list1 = [10, -21, -4, 45, 66, 93, -11] neg_count = len(list(filter(lambda x: (x < 0), list1))) # we can also do len(list1) - neg_count pos_count = len(list(filter(lambda x: (x >= 0), list1))) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive # and negative numbers in a List # list of numbers list1 = [10, -21, -4, 45, 66, 93, -11] neg_count = len(list(filter(lambda x: (x < 0), list1))) # we can also do len(list1) - neg_count pos_count = len(list(filter(lambda x: (x >= 0), list1))) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive # and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, -93, 11] only_pos = [num for num in list1 if num >= 1] pos_count = len(only_pos) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", len(list1) - pos_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive # and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, -93, 11] only_pos = [num for num in list1 if num >= 1] pos_count = len(only_pos) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", len(list1) - pos_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"l = [12, -7, 5, 64, -14] c = 0 x = [a for j, a in enumerate(l) if a >= 0] print(""Length of Positive numbers is:"", len(x)) print(""Length of Negative numbers is:"", len(l) - len(x))","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list l = [12, -7, 5, 64, -14] c = 0 x = [a for j, a in enumerate(l) if a >= 0] print(""Length of Positive numbers is:"", len(x)) print(""Length of Negative numbers is:"", len(l) - len(x)) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] pos_count, neg_count = 0, 0 list2 = list(map(str, list1)) # iterating each number in list for num in list2: # checking condition if num.startswith(""-""): neg_count += 1 elif num != ""0"": if not num.startswith(""-""): pos_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] pos_count, neg_count = 0, 0 list2 = list(map(str, list1)) # iterating each number in list for num in list2: # checking condition if num.startswith(""-""): neg_count += 1 elif num != ""0"": if not num.startswith(""-""): pos_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"l = [12, -7, 5, 64, -14] x = sum(1 for i in l if i >= 0) print(""Length of Positive numbers is:"", x) print(""Length of Negative numbers is:"", len(l) - x)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list l = [12, -7, 5, 64, -14] x = sum(1 for i in l if i >= 0) print(""Length of Positive numbers is:"", x) print(""Length of Negative numbers is:"", len(l) - x) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] pos_count, neg_count = 0, 0 list2 = list(map(str, list1)) # iterating each number in list for num in list2: # checking condition if num[0] == ""-"": neg_count += 1 elif num != ""0"": if not num[0] == ""-"": pos_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] pos_count, neg_count = 0, 0 list2 = list(map(str, list1)) # iterating each number in list for num in list2: # checking condition if num[0] == ""-"": neg_count += 1 elif num != ""0"": if not num[0] == ""-"": pos_count += 1 print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Python program to count positive and negative numbers in a list,https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/,"# Python program to count positive and negative numbers in a List from collections import Counter # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] # use the Counter module to count the number of positive and negative numbers in the list counts = Counter(num > 0 for num in list1) # get the count of positive and negative numbers excluding 0 from positive as well as negative pos_count = counts[True] neg_count = counts[False] - (0 in list1) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count)","Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3","Python program to count positive and negative numbers in a list # Python program to count positive and negative numbers in a List from collections import Counter # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1, 0] # use the Counter module to count the number of positive and negative numbers in the list counts = Counter(num > 0 for num in list1) # get the count of positive and negative numbers excluding 0 from positive as well as negative pos_count = counts[True] neg_count = counts[False] - (0 in list1) print(""Positive numbers in the list: "", pos_count) print(""Negative numbers in the list: "", neg_count) Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # Iterate each element in list # and add them in variable total for ele in list1: if ele % 2 == 0: list1.remove(ele) # printing modified list print(""New list after removing all even numbers: "", list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python # Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # Iterate each element in list # and add them in variable total for ele in list1: if ele % 2 == 0: list1.remove(ele) # printing modified list print(""New list after removing all even numbers: "", list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # will create a new list, # excluding all even numbers list1 = [elem for elem in list1 if elem % 2 != 0] print(*list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python # Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # will create a new list, # excluding all even numbers list1 = [elem for elem in list1 if elem % 2 != 0] print(*list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # removes elements from index 1 to 4 # i.e. 5, 17, 18, 23 will be deleted del list1[1:5] print(*list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python # Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # removes elements from index 1 to 4 # i.e. 5, 17, 18, 23 will be deleted del list1[1:5] print(*list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # items to be removed unwanted_num = {11, 5} list1 = [ele for ele in list1 if ele not in unwanted_num] # printing modified list print(""New list after removing unwanted numbers: "", list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python # Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # items to be removed unwanted_num = {11, 5} list1 = [ele for ele in list1 if ele not in unwanted_num] # printing modified list print(""New list after removing unwanted numbers: "", list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # given index of elements # removes 11, 18, 23 unwanted = [0, 3, 4] for ele in sorted(unwanted, reverse=True): del list1[ele] # printing modified list print(*list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python # Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # given index of elements # removes 11, 18, 23 unwanted = [0, 3, 4] for ele in sorted(unwanted, reverse=True): del list1[ele] # printing modified list print(*list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"list1 = [11, 5, 17, 18, 23, 50] list1 = [elem for i, elem in enumerate(list1) if elem % 2 != 0] print(list1)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python list1 = [11, 5, 17, 18, 23, 50] list1 = [elem for i, elem in enumerate(list1) if elem % 2 != 0] print(list1) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Remove multiple elements from a list in Python,https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/,"import numpy as np # creating a list list1 = [12, 15, 3, 10] # convert list to numpy array arr = np.array(list1) # indices of elements to be removed remove_idx = [0, 2] # use numpy.delete() to remove elements new_arr = np.delete(arr, remove_idx) # convert numpy array back to list new_list = new_arr.tolist() # print the results print(""Removed ="", [list1[i] for i in remove_idx], "", New_List ="", new_list)","Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10]","Remove multiple elements from a list in Python import numpy as np # creating a list list1 = [12, 15, 3, 10] # convert list to numpy array arr = np.array(list1) # indices of elements to be removed remove_idx = [0, 2] # use numpy.delete() to remove elements new_arr = np.delete(arr, remove_idx) # convert numpy array back to list new_list = new_arr.tolist() # print the results print(""Removed ="", [list1[i] for i in remove_idx], "", New_List ="", new_list) Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from a # list of tuples function to remove empty tuples # using list comprehension def Remove(tuples): tuples = [t for t in tuples if t] return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from a # list of tuples function to remove empty tuples # using list comprehension def Remove(tuples): tuples = [t for t in tuples if t] return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python2 program to remove empty tuples# from a list of tuples function to remove# empty tuples using filterdef Remove(tuples):????????????????????????tuples = filter(None, tuples)????????????????????????return tuples??????# Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),???????????????????????????????????????????"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python2 program to remove empty tuples# from a list of tuples function to remove# empty tuples using filterdef Remove(tuples):????????????????????????tuples = filter(None, tuples)????????????????????????return tuples??????# Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),??????????????????????????????????????????? #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using filter def Remove(tuples): tuples = filter(None, tuples) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using filter def Remove(tuples): tuples = filter(None, tuples) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from a # list of tuples function to remove empty tuples # using len() def Remove(tuples): for i in tuples: if len(i) == 0: tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from a # list of tuples function to remove empty tuples # using len() def Remove(tuples): for i in tuples: if len(i) == 0: tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from a # list of tuples function to remove empty tuples def Remove(tuples): for i in tuples: if i == (): tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from a # list of tuples function to remove empty tuples def Remove(tuples): for i in tuples: if i == (): tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] res = [t for i, t in enumerate(tuples) if t] print(res)"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] res = [t for i, t in enumerate(tuples) if t] print(res) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using while loop and in operator def Remove(tuples): while () in tuples: tuples.remove(()) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using while loop and in operator def Remove(tuples): while () in tuples: tuples.remove(()) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from a # list of tuples function to remove empty tuples def Remove(tuples): for i in tuples: if str(i).find(""()"") != -1 and len(str(i)) == 2: tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples))"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from a # list of tuples function to remove empty tuples def Remove(tuples): for i in tuples: if str(i).find(""()"") != -1 and len(str(i)) == 2: tuples.remove(i) return tuples # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(Remove(tuples)) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using lambda function # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] tuples = list(filter(lambda x: len(x) > 0, tuples)) print(tuples)"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # Python program to remove empty tuples from # a list of tuples function to remove empty # tuples using lambda function # Driver Code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] tuples = list(filter(lambda x: len(x) > 0, tuples)) print(tuples) #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"def remove_empty_tuples(tuples): return [t for t in tuples if len(t) > 0] tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(remove_empty_tuples(tuples)) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list def remove_empty_tuples(tuples): return [t for t in tuples if len(t) > 0] tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] print(remove_empty_tuples(tuples)) # This code is contributed by Edula Vinay Kumar Reddy #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"# defining recursive function to remove empty tuples in a list def remove_empty_tuples(start, oldlist, newlist): if start == len(oldlist): # base condition return newlist if oldlist[start] == (): # checking the element is empty tuple or not pass else: newlist.append(oldlist[start]) # appending non empty tuple element to newlist return remove_empty_tuples(start + 1, oldlist, newlist) # recursive function call tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # print('The original list: ',tuples) print(remove_empty_tuples(0, tuples, [])) # This code is contributed by tvsk"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list # defining recursive function to remove empty tuples in a list def remove_empty_tuples(start, oldlist, newlist): if start == len(oldlist): # base condition return newlist if oldlist[start] == (): # checking the element is empty tuple or not pass else: newlist.append(oldlist[start]) # appending non empty tuple element to newlist return remove_empty_tuples(start + 1, oldlist, newlist) # recursive function call tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # print('The original list: ',tuples) print(remove_empty_tuples(0, tuples, [])) # This code is contributed by tvsk #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"import itertools # import the itertools module def Remove(tuples): tuples = list( itertools.filterfalse(lambda x: x == (), tuples) ) # remove empty tuples using filterfalse from itertools return tuples # Driver code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # define the input list of tuples print(Remove(tuples)) # call the Remove function and print the output"," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list import itertools # import the itertools module def Remove(tuples): tuples = list( itertools.filterfalse(lambda x: x == (), tuples) ) # remove empty tuples using filterfalse from itertools return tuples # Driver code tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # define the input list of tuples print(Remove(tuples)) # call the Remove function and print the output #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Remove empty tuples from a list,https://www.geeksforgeeks.org/python-remove-empty-tuples-list/,"from functools import reduce # defining lambda function to remove empty tuples remove_empty = lambda lst, val: lst + [val] if val != () else lst tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # using reduce to remove empty tuples result = reduce(remove_empty, tuples, []) # printing final result print(""The original list is : "" + str(tuples)) print(""The list after removing empty tuples : "" + str(result)) # This code is contributed by Rayudu."," #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]","Python | Remove empty tuples from a list from functools import reduce # defining lambda function to remove empty tuples remove_empty = lambda lst, val: lst + [val] if val != () else lst tuples = [ (), (""ram"", ""15"", ""8""), (), (""laxman"", ""sita""), (""krishna"", ""akbar"", ""45""), ("""", """"), (), ] # using reduce to remove empty tuples result = reduce(remove_empty, tuples, []) # printing final result print(""The original list is : "" + str(tuples)) print(""The list after removing empty tuples : "" + str(result)) # This code is contributed by Rayudu. #Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"# Python program to print # duplicates from a list # of integers def Repeat(x): _size = len(x) repeated = [] for i in range(_size): k = i + 1 for j in range(k, _size): if x[i] == x[j] and x[i] not in repeated: repeated.append(x[i]) return repeated # Driver Code list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] print(Repeat(list1)) # This code is contributed # by Sandeep_anand","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers # Python program to print # duplicates from a list # of integers def Repeat(x): _size = len(x) repeated = [] for i in range(_size): k = i + 1 for j in range(k, _size): if x[i] == x[j] and x[i] not in repeated: repeated.append(x[i]) return repeated # Driver Code list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] print(Repeat(list1)) # This code is contributed # by Sandeep_anand #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"# Python program to print duplicates from # a list of integers lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] uniqueList = [] duplicateList = [] for i in lis: if i not in uniqueList: uniqueList.append(i) elif i not in duplicateList: duplicateList.append(i) print(duplicateList)","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers # Python program to print duplicates from # a list of integers lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] uniqueList = [] duplicateList = [] for i in lis: if i not in uniqueList: uniqueList.append(i) elif i not in duplicateList: duplicateList.append(i) print(duplicateList) #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"from collections import Counter l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] d = Counter(l1) print(d) new_list = list([item for item in d if d[item] > 1]) print(new_list)","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers from collections import Counter l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] d = Counter(l1) print(d) new_list = list([item for item in d if d[item] > 1]) print(new_list) #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"# program to print duplicate numbers in a given list # provided input list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] new = [] # defining output list # condition for reviewing every # element of given input list for a in list: # checking the occurrence of elements n = list.count(a) # if the occurrence is more than # one we add it to the output list if n > 1: if new.count(a) == 0: # condition to check new.append(a) print(new) # This code is contributed by Himanshu Khune","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers # program to print duplicate numbers in a given list # provided input list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] new = [] # defining output list # condition for reviewing every # element of given input list for a in list: # checking the occurrence of elements n = list.count(a) # if the occurrence is more than # one we add it to the output list if n > 1: if new.count(a) == 0: # condition to check new.append(a) print(new) # This code is contributed by Himanshu Khune #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"def duplicate(input_list): return list(set([x for x in input_list if input_list.count(x) > 1])) if __name__ == ""__main__"": input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(duplicate(input_list)) # This code is contributed by saikot","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers def duplicate(input_list): return list(set([x for x in input_list if input_list.count(x) > 1])) if __name__ == ""__main__"": input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(duplicate(input_list)) # This code is contributed by saikot #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"def duplicate(input_list): new_dict, new_list = {}, [] for i in input_list: if not i in new_dict: new_dict[i] = 1 else: new_dict[i] += 1 for key, values in new_dict.items(): if values > 1: new_list.append(key) return new_list if __name__ == ""__main__"": input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(duplicate(input_list)) # This code is contributed by saikot","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers def duplicate(input_list): new_dict, new_list = {}, [] for i in input_list: if not i in new_dict: new_dict[i] = 1 else: new_dict[i] += 1 for key, values in new_dict.items(): if values > 1: new_list.append(key) return new_list if __name__ == ""__main__"": input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(duplicate(input_list)) # This code is contributed by saikot #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] x = [] y = [] for i in lis: if i not in x: x.append(i) for i in x: if lis.count(i) > 1: y.append(i) print(y)","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] x = [] y = [] for i in lis: if i not in x: x.append(i) for i in x: if lis.count(i) > 1: y.append(i) print(y) #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(list(set([x for i, x in enumerate(input_list) if input_list.count(x) > 1])))","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] print(list(set([x for i, x in enumerate(input_list) if input_list.count(x) > 1]))) #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"import operator as op # program to print duplicate numbers in a given list # provided input list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] new = [] # defining output list # condition for reviewing every # element of given input list for a in list: # checking the occurrence of elements n = op.countOf(list, a) # if the occurrence is more than # one we add it to the output list if n > 1: if op.countOf(new, a) == 0: # condition to check new.append(a) print(new) # This code is contributed by vikkycirus","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers import operator as op # program to print duplicate numbers in a given list # provided input list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] new = [] # defining output list # condition for reviewing every # element of given input list for a in list: # checking the occurrence of elements n = op.countOf(list, a) # if the occurrence is more than # one we add it to the output list if n > 1: if op.countOf(new, a) == 0: # condition to check new.append(a) print(new) # This code is contributed by vikkycirus #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] x = [] y = [] import operator for i in lis: if i not in x: x.append(i) for i in x: if operator.countOf(lis, i) > 1: y.append(i) print(y)","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] x = [] y = [] import operator for i in lis: if i not in x: x.append(i) for i in x: if operator.countOf(lis, i) > 1: y.append(i) print(y) #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Python | Program to print duplicates from a list of integers,https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/,"import numpy as np l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] unique, counts = np.unique(l1, return_counts=True) new_list = unique[np.where(counts > 1)] print(new_list) # This code is contributed by Rayudu","#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60]","Python | Program to print duplicates from a list of integers import numpy as np l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9] unique, counts = np.unique(l1, return_counts=True) new_list = unique[np.where(counts > 1)] print(new_list) # This code is contributed by Rayudu #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] #Output : output_list = [20, 30, -20, 60] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # from List using list comprehension # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List # using list comprehension res = [ele for ele in test_list if ele != []] # printing result print(""List after empty list removal : "" + str(res))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # from List using list comprehension # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List # using list comprehension res = [ele for ele in test_list if ele != []] # printing result print(""List after empty list removal : "" + str(res)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 Code to Demonstrate Remove empty List # from List using filter() Method # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using filter() method res = list(filter(None, test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 Code to Demonstrate Remove empty List # from List using filter() Method # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using filter() method res = list(filter(None, test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python Code to Remove empty List from List def empty_list_remove(input_list): new_list = [] for ele in input_list: if ele: new_list.append(ele) return new_list # input list values input_list = [5, 6, [], 3, [], [], 9] # print initial list values print(f""The original list is : {input_list}"") # function-call & print values print(f""List after empty list removal : {empty_list_remove(input_list)}"")"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python Code to Remove empty List from List def empty_list_remove(input_list): new_list = [] for ele in input_list: if ele: new_list.append(ele) return new_list # input list values input_list = [5, 6, [], 3, [], [], 9] # print initial list values print(f""The original list is : {input_list}"") # function-call & print values print(f""List after empty list removal : {empty_list_remove(input_list)}"") #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) new_list = [] # Remove empty List from List for i in test_list: x = str(type(i)) if x.find(""list"") != -1: if len(i) != 0: new_list.append(i) else: new_list.append(i) # printing result print(""List after empty list removal : "" + str(new_list))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) new_list = [] # Remove empty List from List for i in test_list: x = str(type(i)) if x.find(""list"") != -1: if len(i) != 0: new_list.append(i) else: new_list.append(i) # printing result print(""List after empty list removal : "" + str(new_list)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List while [] in test_list: test_list.remove([]) # printing result print(""List after empty list removal : "" + str(test_list))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List while [] in test_list: test_list.remove([]) # printing result print(""List after empty list removal : "" + str(test_list)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) x = list(map(str, test_list)) y = """".join(x) y = y.replace(""[]"", """") y = list(map(int, y)) # printing result print(""List after empty list removal : "" + str(y))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) x = list(map(str, test_list)) y = """".join(x) y = y.replace(""[]"", """") y = list(map(int, y)) # printing result print(""List after empty list removal : "" + str(y)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"test_list = [5, 6, [], 3, [], [], 9] res = [ele for i, ele in enumerate(test_list) if ele != []] print(res)"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List test_list = [5, 6, [], 3, [], [], 9] res = [ele for i, ele in enumerate(test_list) if ele != []] print(res) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List res = filter(None, test_list) # printing result print(""List after empty list removal : "", res)"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List res = filter(None, test_list) # printing result print(""List after empty list removal : "", res) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 Code to Demonstrate Remove empty List # from List using lambda function # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using lambda function res = list(filter(lambda x: x != [], test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 Code to Demonstrate Remove empty List # from List using lambda function # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using lambda function res = list(filter(lambda x: x != [], test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 code to Demonstrate Remove empty List # defining recursive function to remove empty list def remove_empty(start, oldlist, newlist): if start == len(oldlist): # base condition return newlist if oldlist[start] == []: # checking the element is empty list or not pass else: newlist.append(oldlist[start]) # appending non empty list element to newlist return remove_empty(start + 1, oldlist, newlist) # recursive function call test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) result = remove_empty(0, test_list, []) # printing result print(""List after empty list removal : "", result)"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 code to Demonstrate Remove empty List # defining recursive function to remove empty list def remove_empty(start, oldlist, newlist): if start == len(oldlist): # base condition return newlist if oldlist[start] == []: # checking the element is empty list or not pass else: newlist.append(oldlist[start]) # appending non empty list element to newlist return remove_empty(start + 1, oldlist, newlist) # recursive function call test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) result = remove_empty(0, test_list, []) # printing result print(""List after empty list removal : "", result) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Python3 Code to Demonstrate Remove empty List # from List import itertools # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using lambda function res = list(itertools.filterfalse(lambda x: x == [], test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res))"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Python3 Code to Demonstrate Remove empty List # from List import itertools # Initializing list by custom values test_list = [5, 6, [], 3, [], [], 9] # Printing original list print(""The original list is : "" + str(test_list)) # Removing empty List from List # using lambda function res = list(itertools.filterfalse(lambda x: x == [], test_list)) # Printing the resultant list print(""List after empty list removal : "" + str(res)) #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List # using map() function res = list(map(lambda x: x if x != [] else None, test_list)) res = [x for x in res if x != None] # printing result print(""List after empty list removal : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Initializing list test_list = [5, 6, [], 3, [], [], 9] # printing original list print(""The original list is : "" + str(test_list)) # Remove empty List from List # using map() function res = list(map(lambda x: x if x != [] else None, test_list)) res = [x for x in res if x != None] # printing result print(""List after empty list removal : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"import re # input list values input_list = [5, 6, [], 3, [], [], 9] # print initial list values print(f""The original list is : {input_list}"") # removing empty list from list res = list(filter(None, [x for x in input_list if not re.match(""\[\]"", str(x))])) # print resultant list print(f""List after empty list removal : {res}"")"," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List import re # input list values input_list = [5, 6, [], 3, [], [], 9] # print initial list values print(f""The original list is : {input_list}"") # removing empty list from list res = list(filter(None, [x for x in input_list if not re.match(""\[\]"", str(x))])) # print resultant list print(f""List after empty list removal : {res}"") #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Remove empty List from List,https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar,"# Define the test list test_list = [5, 6, [], 3, [], [], 9] # Print the original list print(""The original list is : "" + str(test_list)) # Counter variable 'i' to keep track of the current index i = 0 # While loop to go through all elements of the list while i < len(test_list): # If the current element is an empty list, remove it from the list if test_list[i] == []: test_list.pop(i) # Else, increment the counter variable else: i += 1 # Reassign the result to the original list after removing all empty lists res = test_list # Print the result print(""List after empty list removal : "" + str(res)) # This code is contributed by Jyothi pinjala."," #Output : The original list is : [5, 6, [], 3, [], [], 9]","Remove empty List from List # Define the test list test_list = [5, 6, [], 3, [], [], 9] # Print the original list print(""The original list is : "" + str(test_list)) # Counter variable 'i' to keep track of the current index i = 0 # While loop to go through all elements of the list while i < len(test_list): # If the current element is an empty list, remove it from the list if test_list[i] == []: test_list.pop(i) # Else, increment the counter variable else: i += 1 # Reassign the result to the original list after removing all empty lists res = test_list # Print the result print(""List after empty list removal : "" + str(res)) # This code is contributed by Jyothi pinjala. #Output : The original list is : [5, 6, [], 3, [], [], 9] [END]" Python - Convert List to List of dictionaries,https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/,"# Python3 code to demonstrate working of # Convert List to List of dictionaries # Using dictionary comprehension + loop # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # loop to iterate through elements # using dictionary comprehension # for dictionary construction n = len(test_list) res = [] for idx in range(0, n, 2): res.append({key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]}) # printing result print(""The constructed dictionary list : "" + str(res))"," #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]","Python - Convert List to List of dictionaries # Python3 code to demonstrate working of # Convert List to List of dictionaries # Using dictionary comprehension + loop # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # loop to iterate through elements # using dictionary comprehension # for dictionary construction n = len(test_list) res = [] for idx in range(0, n, 2): res.append({key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]}) # printing result print(""The constructed dictionary list : "" + str(res)) #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] [END]" Python - Convert List to List of dictionaries,https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/,"# Python3 code to demonstrate working of # Convert List to List of dictionaries # Using zip() + list comprehension # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # using list comprehension to perform as shorthand n = len(test_list) res = [ {key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]} for idx in range(0, n, 2) ] # printing result print(""The constructed dictionary list : "" + str(res))"," #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]","Python - Convert List to List of dictionaries # Python3 code to demonstrate working of # Convert List to List of dictionaries # Using zip() + list comprehension # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # using list comprehension to perform as shorthand n = len(test_list) res = [ {key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]} for idx in range(0, n, 2) ] # printing result print(""The constructed dictionary list : "" + str(res)) #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] [END]" Python - Convert List to List of dictionaries,https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/,"# initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # initializing key list key_list = [""name"", ""number""] # using zip() function and dictionary comprehension res = [ {key_list[i]: val for i, val in enumerate(pair)} for pair in zip(test_list[::2], test_list[1::2]) ] # printing result print(""The constructed dictionary list : "" + str(res))"," #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]","Python - Convert List to List of dictionaries # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # initializing key list key_list = [""name"", ""number""] # using zip() function and dictionary comprehension res = [ {key_list[i]: val for i, val in enumerate(pair)} for pair in zip(test_list[::2], test_list[1::2]) ] # printing result print(""The constructed dictionary list : "" + str(res)) #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] [END]" Python - Convert List to List of dictionaries,https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/,"# Python3 code to demonstrate working of # Convert List to List of dictionaries # Using groupby() function from itertools module # import itertools module import itertools # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # using groupby() function to group elements into pairs res = [] for pair in zip(test_list[::2], test_list[1::2]): res.append({key_list[0]: pair[0], key_list[1]: pair[1]}) # printing result print(""The constructed dictionary list : "" + str(res))"," #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]","Python - Convert List to List of dictionaries # Python3 code to demonstrate working of # Convert List to List of dictionaries # Using groupby() function from itertools module # import itertools module import itertools # initializing lists test_list = [""Gfg"", 3, ""is"", 8, ""Best"", 10, ""for"", 18, ""Geeks"", 33] # printing original list print(""The original list : "" + str(test_list)) # initializing key list key_list = [""name"", ""number""] # using groupby() function to group elements into pairs res = [] for pair in zip(test_list[::2], test_list[1::2]): res.append({key_list[0]: pair[0], key_list[1]: pair[1]}) # printing result print(""The constructed dictionary list : "" + str(res)) #Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] [END]" Python - Convert Lists of List to Dictionary,https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/,"# Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using loop # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using loop res = dict() for sub in test_list: res[tuple(sub[:2])] = tuple(sub[2:]) # printing result print(""The mapped Dictionary : "" + str(res))"," #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}","Python - Convert Lists of List to Dictionary # Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using loop # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using loop res = dict() for sub in test_list: res[tuple(sub[:2])] = tuple(sub[2:]) # printing result print(""The mapped Dictionary : "" + str(res)) #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} [END]" Python - Convert Lists of List to Dictionary,https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/,"# Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using dictionary comprehension # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using dictionary comprehension res = {tuple(sub[:2]): tuple(sub[2:]) for sub in test_list} # printing result print(""The mapped Dictionary : "" + str(res))"," #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}","Python - Convert Lists of List to Dictionary # Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using dictionary comprehension # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using dictionary comprehension res = {tuple(sub[:2]): tuple(sub[2:]) for sub in test_list} # printing result print(""The mapped Dictionary : "" + str(res)) #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} [END]" Python - Convert Lists of List to Dictionary,https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/,"original_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] mapped_dict = {(lst[0], lst[1]): tuple(lst[2:]) for lst in original_list} print(""The mapped Dictionary :"", mapped_dict)"," #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}","Python - Convert Lists of List to Dictionary original_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] mapped_dict = {(lst[0], lst[1]): tuple(lst[2:]) for lst in original_list} print(""The mapped Dictionary :"", mapped_dict) #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} [END]" Python - Convert Lists of List to Dictionary,https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/,"# Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using zip() and loop # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using zip() and loop result_dict = {} for sublist in test_list: key = tuple(sublist[:2]) value = tuple(sublist[2:]) result_dict[key] = value # printing result print(""The mapped Dictionary : "" + str(result_dict))"," #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}","Python - Convert Lists of List to Dictionary # Python3 code to demonstrate working of # Convert Lists of List to Dictionary # Using zip() and loop # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Lists of List to Dictionary # Using zip() and loop result_dict = {} for sublist in test_list: key = tuple(sublist[:2]) value = tuple(sublist[2:]) result_dict[key] = value # printing result print(""The mapped Dictionary : "" + str(result_dict)) #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} [END]" Python - Convert Lists of List to Dictionary,https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/,"from functools import reduce # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is: "" + str(test_list)) # define function to combine dictionaries def combine_dicts(dict1, dict2): dict1.update(dict2) return dict1 # use reduce to apply combine_dicts to all nested dictionaries res = reduce(combine_dicts, [{tuple(sub[:2]): tuple(sub[2:])} for sub in test_list]) # print mapped dictionary print(""The mapped dictionary: "" + str(res))"," #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}","Python - Convert Lists of List to Dictionary from functools import reduce # initializing list test_list = [[""a"", ""b"", 1, 2], [""c"", ""d"", 3, 4], [""e"", ""f"", 5, 6]] # printing original list print(""The original list is: "" + str(test_list)) # define function to combine dictionaries def combine_dicts(dict1, dict2): dict1.update(dict2) return dict1 # use reduce to apply combine_dicts to all nested dictionaries res = reduce(combine_dicts, [{tuple(sub[:2]): tuple(sub[2:])} for sub in test_list]) # print mapped dictionary print(""The mapped dictionary: "" + str(res)) #Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} [END]" Python | Uncommon elements in Lists of List,https://www.geeksforgeeks.org/python-uncommon-elements-in-lists-of-list/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Uncommon elements in List # using naive method # initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) # using naive method # Uncommon elements in List res_list = [] for i in test_list1: if i not in test_list2: res_list.append(i) for i in test_list2: if i not in test_list1: res_list.append(i) # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list))"," #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]]","Python | Uncommon elements in Lists of List # Python 3 code to demonstrate # Uncommon elements in List # using naive method # initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) # using naive method # Uncommon elements in List res_list = [] for i in test_list1: if i not in test_list2: res_list.append(i) for i in test_list2: if i not in test_list1: res_list.append(i) # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list)) #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]] [END]" Python | Uncommon elements in Lists of List,https://www.geeksforgeeks.org/python-uncommon-elements-in-lists-of-list/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Uncommon elements in Lists of List # using map() + set() + ^ # initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) # using map() + set() + ^ # Uncommon elements in Lists of List res_set = set(map(tuple, test_list1)) ^ set(map(tuple, test_list2)) res_list = list(map(list, res_set)) # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list))"," #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]]","Python | Uncommon elements in Lists of List # Python 3 code to demonstrate # Uncommon elements in Lists of List # using map() + set() + ^ # initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) # using map() + set() + ^ # Uncommon elements in Lists of List res_set = set(map(tuple, test_list1)) ^ set(map(tuple, test_list2)) res_list = list(map(list, res_set)) # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list)) #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]] [END]" Python | Uncommon elements in Lists of List,https://www.geeksforgeeks.org/python-uncommon-elements-in-lists-of-list/?ref=leftbar-rightbar,"# initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) res_list = [x for x in test_list1 if x not in test_list2] + [ y for y in test_list2 if y not in test_list1 ] # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list)) # This code is contributed by Jyothi pinjala"," #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]]","Python | Uncommon elements in Lists of List # initializing lists test_list1 = [[1, 2], [3, 4], [5, 6]] test_list2 = [[3, 4], [5, 7], [1, 2]] # printing both lists print(""The original list 1 : "" + str(test_list1)) print(""The original list 2 : "" + str(test_list2)) res_list = [x for x in test_list1 if x not in test_list2] + [ y for y in test_list2 if y not in test_list1 ] # printing the uncommon print(""The uncommon of two lists is : "" + str(res_list)) # This code is contributed by Jyothi pinjala #Output : The original list 1 : [[1, 2], [3, 4], [5, 6]] [END]" Python program to select Random value from list of lists,https://www.geeksforgeeks.org/python-program-to-select-random-value-form-list-of-lists/,"# Python3 code to demonstrate working of # Random Matrix Element # Using chain.from_iterables() + random.choice() from itertools import chain import random # Initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # Printing original list print(""The original list is : "" + str(test_list)) # choice() for random number, from_iterables for flattening res = random.choice(list(chain.from_iterable(test_list))) # Printing result print(""Random number from Matrix : "" + str(res))","#Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7","Python program to select Random value from list of lists # Python3 code to demonstrate working of # Random Matrix Element # Using chain.from_iterables() + random.choice() from itertools import chain import random # Initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # Printing original list print(""The original list is : "" + str(test_list)) # choice() for random number, from_iterables for flattening res = random.choice(list(chain.from_iterable(test_list))) # Printing result print(""Random number from Matrix : "" + str(res)) #Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7 [END]" Python program to select Random value from list of lists,https://www.geeksforgeeks.org/python-program-to-select-random-value-form-list-of-lists/,"# Python3 code to demonstrate working of # Random Matrix Element # Using random.choice() [if row number given] import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # initializing Row number r_no = [0, 1, 2] # choice() for random number, from_iterables for flattening res = random.choice(test_list[random.choice(r_no)]) # printing result print(""Random number from Matrix Row : "" + str(res))","#Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7","Python program to select Random value from list of lists # Python3 code to demonstrate working of # Random Matrix Element # Using random.choice() [if row number given] import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # initializing Row number r_no = [0, 1, 2] # choice() for random number, from_iterables for flattening res = random.choice(test_list[random.choice(r_no)]) # printing result print(""Random number from Matrix Row : "" + str(res)) #Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7 [END]" Python program to select Random value from list of lists,https://www.geeksforgeeks.org/python-program-to-select-random-value-form-list-of-lists/,"import numpy as np import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # initializing Row number r_no = [0, 1, 2] # Converting list to numpy array arr = np.array(test_list) # Generating random number from matrix # using numpy random.choice() method res = np.random.choice(arr[random.choice(r_no)]) # Printing result print(""Random number from Matrix Row : "" + str(res))","#Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7","Python program to select Random value from list of lists import numpy as np import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # initializing Row number r_no = [0, 1, 2] # Converting list to numpy array arr = np.array(test_list) # Generating random number from matrix # using numpy random.choice() method res = np.random.choice(arr[random.choice(r_no)]) # Printing result print(""Random number from Matrix Row : "" + str(res)) #Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7 [END]" Python program to select Random value from list of lists,https://www.geeksforgeeks.org/python-program-to-select-random-value-form-list-of-lists/,"import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # generating random row index row_index = random.randint(0, len(test_list) - 1) # extracting the row corresponding to the random index using list comprehension row = [test_list[row_index][i] for i in range(len(test_list[row_index]))] # getting a sample of elements from the row using random.sample() sample = random.sample(row, k=1) # getting the randomly selected element from the sample res = sample[0] # printing result print(""Random number from Matrix Row : "" + str(res))","#Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7","Python program to select Random value from list of lists import random # initializing list test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] # printing original list print(""The original list is : "" + str(test_list)) # generating random row index row_index = random.randint(0, len(test_list) - 1) # extracting the row corresponding to the random index using list comprehension row = [test_list[row_index][i] for i in range(len(test_list[row_index]))] # getting a sample of elements from the row using random.sample() sample = random.sample(row, k=1) # getting the randomly selected element from the sample res = sample[0] # printing result print(""Random number from Matrix Row : "" + str(res)) #Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]] #Output : 7 [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"# Python3 code to demonstrate # Reverse Row sort in Lists of List # using loop # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using loop for ele in test_list: ele.sort(reverse=True) # printing result print(""The reverse sorted Matrix is : "" + str(test_list))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List # Python3 code to demonstrate # Reverse Row sort in Lists of List # using loop # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using loop for ele in test_list: ele.sort(reverse=True) # printing result print(""The reverse sorted Matrix is : "" + str(test_list)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"# Python3 code to demonstrate # Reverse Row sort in Lists of List # using list comprehension + sorted() # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using list comprehension + sorted() res = [sorted(sub, reverse=True) for sub in test_list] # printing result print(""The reverse sorted Matrix is : "" + str(res))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List # Python3 code to demonstrate # Reverse Row sort in Lists of List # using list comprehension + sorted() # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using list comprehension + sorted() res = [sorted(sub, reverse=True) for sub in test_list] # printing result print(""The reverse sorted Matrix is : "" + str(res)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"# Python3 code to demonstrate # Reverse Row sort in Lists of List # using map + sorted() # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using map + sorted() res = list(map(lambda x: sorted(x, reverse=True), test_list)) # printing result print(""The reverse sorted Matrix is : "" + str(res))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List # Python3 code to demonstrate # Reverse Row sort in Lists of List # using map + sorted() # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using map + sorted() res = list(map(lambda x: sorted(x, reverse=True), test_list)) # printing result print(""The reverse sorted Matrix is : "" + str(res)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"# Python3 code to demonstrate # Reverse Row sort in Lists of List # using loop # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using loop res = [] for ele in test_list: ele.sort() res.append(ele[::-1]) # printing result print(""The reverse sorted Matrix is : "" + str(res))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List # Python3 code to demonstrate # Reverse Row sort in Lists of List # using loop # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using loop res = [] for ele in test_list: ele.sort() res.append(ele[::-1]) # printing result print(""The reverse sorted Matrix is : "" + str(res)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"def reverse_sort_matrix(matrix): return list(map(lambda row: sorted(row, reverse=True), matrix)) matrix = [[4, 1, 6], [7, 8], [4, 10, 8]] print(reverse_sort_matrix(matrix))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List def reverse_sort_matrix(matrix): return list(map(lambda row: sorted(row, reverse=True), matrix)) matrix = [[4, 1, 6], [7, 8], [4, 10, 8]] print(reverse_sort_matrix(matrix)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Reverse Row sort in Lists of List,https://www.geeksforgeeks.org/python-reverse-row-sort-in-lists-of-list/,"# Python3 code to demonstrate # Reverse Row sort in Lists of List # using the heapq module import heapq # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using the heapq module res = [] for sublist in test_list: heapq.heapify(sublist) res.append([heapq.heappop(sublist) for i in range(len(sublist))][::-1]) # printing result print(""The reverse sorted Matrix is : "" + str(res))"," #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]","Python - Reverse Row sort in Lists of List # Python3 code to demonstrate # Reverse Row sort in Lists of List # using the heapq module import heapq # initializing list test_list = [[4, 1, 6], [7, 8], [4, 10, 8]] # printing original list print(""The original list is : "" + str(test_list)) # Reverse Row sort in Lists of List # using the heapq module res = [] for sublist in test_list: heapq.heapify(sublist) res.append([heapq.heappop(sublist) for i in range(len(sublist))][::-1]) # printing result print(""The reverse sorted Matrix is : "" + str(res)) #Output : The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using list comprehension # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using list comprehension res = [] for sub in test_list: res.append([[ele, sub[-1]] for ele in sub[:-1]]) # printing result print(""The list after pairing is : "" + str(res))"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using list comprehension # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using list comprehension res = [] for sub in test_list: res.append([[ele, sub[-1]] for ele in sub[:-1]]) # printing result print(""The list after pairing is : "" + str(res)) #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using product() + loop from itertools import product # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using product() + loop res = [] for idx in test_list: res.append(list(product(idx[:-1], [idx[-1]]))) # printing result print(""The list after pairing is : "" + str(res))"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using product() + loop from itertools import product # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using product() + loop res = [] for idx in test_list: res.append(list(product(idx[:-1], [idx[-1]]))) # printing result print(""The list after pairing is : "" + str(res)) #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using zip function # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using zip function res = [] for sub in test_list: res.append(list(zip(sub[:-1], [sub[-1]] * (len(sub) - 1)))) # printing result print(""The list after pairing is : "" + str(res)) # this code contributed by tvsk"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using zip function # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using zip function res = [] for sub in test_list: res.append(list(zip(sub[:-1], [sub[-1]] * (len(sub) - 1)))) # printing result print(""The list after pairing is : "" + str(res)) # this code contributed by tvsk #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using map() and zip_longest() from itertools import zip_longest # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using map() and zip_longest() res = [ list(map(lambda x: (x[0], sub[-1]), zip_longest(sub[:-1], [], fillvalue=sub[-1]))) for sub in test_list ] # printing result print(""The list after pairing is : "" + str(res))"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using map() and zip_longest() from itertools import zip_longest # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using map() and zip_longest() res = [ list(map(lambda x: (x[0], sub[-1]), zip_longest(sub[:-1], [], fillvalue=sub[-1]))) for sub in test_list ] # printing result print(""The list after pairing is : "" + str(res)) #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using nested loop # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using nested loop res = [] for sub in test_list: temp = [] for i in range(len(sub) - 1): temp.append([sub[i], sub[-1]]) res.append(temp) # printing result print(""The list after pairing is : "" + str(res))"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using nested loop # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row # using nested loop res = [] for sub in test_list: temp = [] for i in range(len(sub) - 1): temp.append([sub[i], sub[-1]]) res.append(temp) # printing result print(""The list after pairing is : "" + str(res)) #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python - Pair elements with Rear element in Matrix Row,https://www.geeksforgeeks.org/python-pair-elements-with-rear-element-in-matrix-row/,"# Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using list comprehension with tuple packing and unpacking # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # Printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row using list comprehension with tuple packing and unpacking res = [[(ele, sub[-1]) for ele in sub[:-1]] for sub in test_list] # Printing result print(""The list after pairing is : "" + str(res))"," #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]","Python - Pair elements with Rear element in Matrix Row # Python3 code to demonstrate # Pair elements with Rear element in Matrix Row # using list comprehension with tuple packing and unpacking # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # Printing original list print(""The original list is : "" + str(test_list)) # Pair elements with Rear element in Matrix Row using list comprehension with tuple packing and unpacking res = [[(ele, sub[-1]) for ele in sub[:-1]] for sub in test_list] # Printing result print(""The list after pairing is : "" + str(res)) #Output : The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]] [END]" Python Program to count unique values inside a list,https://www.geeksforgeeks.org/how-to-count-unique-values-inside-a-list/,"# taking an input list input_list = [1, 2, 2, 5, 8, 4, 4, 8] # taking an input list l1 = [] # taking an counter count = 0 # traversing the array for item in input_list: if item not in l1: count += 1 l1.append(item) # printing the output print(""No of unique items are:"", count)"," #Output : No of unique items are: 5","Python Program to count unique values inside a list # taking an input list input_list = [1, 2, 2, 5, 8, 4, 4, 8] # taking an input list l1 = [] # taking an counter count = 0 # traversing the array for item in input_list: if item not in l1: count += 1 l1.append(item) # printing the output print(""No of unique items are:"", count) #Output : No of unique items are: 5 [END]" Python Program to count unique values inside a list,https://www.geeksforgeeks.org/how-to-count-unique-values-inside-a-list/,"# importing Counter module from collections import Counter input_list = [1, 2, 2, 5, 8, 4, 4, 8] # creating a list with the keys items = Counter(input_list).keys() print(""No of unique items in the list are:"", len(items))"," #Output : No of unique items are: 5","Python Program to count unique values inside a list # importing Counter module from collections import Counter input_list = [1, 2, 2, 5, 8, 4, 4, 8] # creating a list with the keys items = Counter(input_list).keys() print(""No of unique items in the list are:"", len(items)) #Output : No of unique items are: 5 [END]" Python Program to count unique values inside a list,https://www.geeksforgeeks.org/how-to-count-unique-values-inside-a-list/,"input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to set new_set = set(input_list) print(""No of unique items in the list are:"", len(new_set))"," #Output : No of unique items are: 5","Python Program to count unique values inside a list input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to set new_set = set(input_list) print(""No of unique items in the list are:"", len(new_set)) #Output : No of unique items are: 5 [END]" Python Program to count unique values inside a list,https://www.geeksforgeeks.org/how-to-count-unique-values-inside-a-list/,"input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to filter list new_set = [x for i, x in enumerate(input_list) if x not in input_list[:i]] print(""No of unique items in the list are:"", len(new_set))"," #Output : No of unique items are: 5","Python Program to count unique values inside a list input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to filter list new_set = [x for i, x in enumerate(input_list) if x not in input_list[:i]] print(""No of unique items in the list are:"", len(new_set)) #Output : No of unique items are: 5 [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"# Python 3 code to demonstrate # Duplication Removal List Product # using naive methods # getting Product def prod(val): res = 1 for ele in val: res *= ele return res # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using naive method # Duplication Removal List Product res = [] for i in test_list: if i not in res: res.append(i) res = prod(res) # printing list after removal print(""Duplication removal list product : "" + str(res))"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates # Python 3 code to demonstrate # Duplication Removal List Product # using naive methods # getting Product def prod(val): res = 1 for ele in val: res *= ele return res # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using naive method # Duplication Removal List Product res = [] for i in test_list: if i not in res: res.append(i) res = prod(res) # printing list after removal print(""Duplication removal list product : "" + str(res)) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"# Python 3 code to demonstrate # Duplication Removal List Product # using list comprehension # getting Product def prod(val): res = 1 for ele in val: res *= ele return res # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using list comprehension # Duplication Removal List Product res = [] [res.append(x) for x in test_list if x not in res] res = prod(res) # printing list after removal print(""Duplication removal list product : "" + str(res))"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates # Python 3 code to demonstrate # Duplication Removal List Product # using list comprehension # getting Product def prod(val): res = 1 for ele in val: res *= ele return res # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using list comprehension # Duplication Removal List Product res = [] [res.append(x) for x in test_list if x not in res] res = prod(res) # printing list after removal print(""Duplication removal list product : "" + str(res)) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"import functools functools.reduce(lambda x, y: x * y, set([1, 3, 5, 6, 3, 5, 6, 1]), 1)"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates import functools functools.reduce(lambda x, y: x * y, set([1, 3, 5, 6, 3, 5, 6, 1]), 1) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"# Python 3 code to demonstrate # Duplication Removal List Product test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using naive method # Duplication Removal List Product x = list(set(test_list)) prod = 1 for i in x: prod *= i # printing list after removal print(""Duplication removal list product : "" + str(prod))"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates # Python 3 code to demonstrate # Duplication Removal List Product test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # using naive method # Duplication Removal List Product x = list(set(test_list)) prod = 1 for i in x: prod *= i # printing list after removal print(""Duplication removal list product : "" + str(prod)) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"import math def product(list): s = set(list) return math.prod(s) l = [1, 3, 5, 6, 3, 5, 6, 1] print(product(l))"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates import math def product(list): s = set(list) return math.prod(s) l = [1, 3, 5, 6, 3, 5, 6, 1] print(product(l)) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - List product excluding duplicates,https://www.geeksforgeeks.org/python-list-product-excluding-duplicates/,"import numpy as np # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # converting list to numpy array and finding unique elements unique_array = np.unique(np.array(test_list)) # finding product of unique elements result = np.prod(unique_array) # printing result print(""Duplication removal list product : "" + str(result))"," #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1]","Python - List product excluding duplicates import numpy as np # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print(""The original list is : "" + str(test_list)) # converting list to numpy array and finding unique elements unique_array = np.unique(np.array(test_list)) # finding product of unique elements result = np.prod(unique_array) # printing result print(""Duplication removal list product : "" + str(result)) #Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"# Python3 code to demonstrate working of # Extract elements with Frequency greater than K # Using count() + loop # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 res = [] for i in test_list: # using count() to get count of elements freq = test_list.count(i) # checking if not already entered in results if freq > K and i not in res: res.append(i) # printing results print(""The required elements : "" + str(res))"," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K # Python3 code to demonstrate working of # Extract elements with Frequency greater than K # Using count() + loop # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 res = [] for i in test_list: # using count() to get count of elements freq = test_list.count(i) # checking if not already entered in results if freq > K and i not in res: res.append(i) # printing results print(""The required elements : "" + str(res)) #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"# Python3 code to demonstrate working of # Extract elements with Frequency greater than K # Using list comprehension + Counter() from collections import Counter # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 # using list comprehension to bind result res = [ele for ele, cnt in Counter(test_list).items() if cnt > K] # printing results print(""The required elements : "" + str(res))"," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K # Python3 code to demonstrate working of # Extract elements with Frequency greater than K # Using list comprehension + Counter() from collections import Counter # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 # using list comprehension to bind result res = [ele for ele, cnt in Counter(test_list).items() if cnt > K] # printing results print(""The required elements : "" + str(res)) #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6] k = 2 unique_elems = [] freq_dict = {} output = [] # printing string print(""The original list : "" + str(test_list)) for i in test_list: # Append in the unique element list if i not in unique_elems: unique_elems.append(i) freq_dict[i] = 1 else: # increment the counter if element is duplicate freq_dict[i] += 1 # Add in the output list only once if freq_dict[i] == k + 1: output.append(i) print(""The required elements : "", str(output))"," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6] k = 2 unique_elems = [] freq_dict = {} output = [] # printing string print(""The original list : "" + str(test_list)) for i in test_list: # Append in the unique element list if i not in unique_elems: unique_elems.append(i) freq_dict[i] = 1 else: # increment the counter if element is duplicate freq_dict[i] += 1 # Add in the output list only once if freq_dict[i] == k + 1: output.append(i) print(""The required elements : "", str(output)) #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"# Python3 code to demonstrate working of # Extract elements with Frequency greater than K import operator as op # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 unique_elements = set(test_list) res = [] for i in unique_elements: # using operatorcountOf() to get count of elements if op.countOf(test_list, i) > K: res.append(i) # printing results print(""The required elements : "" + str(res))"," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K # Python3 code to demonstrate working of # Extract elements with Frequency greater than K import operator as op # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) # initializing K K = 2 unique_elements = set(test_list) res = [] for i in unique_elements: # using operatorcountOf() to get count of elements if op.countOf(test_list, i) > K: res.append(i) # printing results print(""The required elements : "" + str(res)) #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"import numpy as np test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] K = 2 # use numpy unique to extract unique elements and their frequency unique_elements, counts = np.unique(test_list, return_counts=True) # extract elements with frequency greater than K res = unique_elements[counts > K].tolist() # printing results print(""The required elements : "" + str(res)) # this code is contributed by Asif_Shaik"," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K import numpy as np test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] K = 2 # use numpy unique to extract unique elements and their frequency unique_elements, counts = np.unique(test_list, return_counts=True) # extract elements with frequency greater than K res = unique_elements[counts > K].tolist() # printing results print(""The required elements : "" + str(res)) # this code is contributed by Asif_Shaik #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"from collections import Counter test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] K = 2 freq_dict = Counter(test_list) res = list(filter(lambda ele: freq_dict[ele] > K, freq_dict)) print(""The required elements : "" + str(res)) # This code is contributed By Vinay Pinjala."," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K from collections import Counter test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] K = 2 freq_dict = Counter(test_list) res = list(filter(lambda ele: freq_dict[ele] > K, freq_dict)) print(""The required elements : "" + str(res)) # This code is contributed By Vinay Pinjala. #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Extract elements with Frequency greater than K ,https://www.geeksforgeeks.org/python-extract-elements-with-frequency-greater-than-k/,"from collections import defaultdict # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) freq_dict = defaultdict(int) for num in test_list: freq_dict[num] += 1 K = 2 res = [] for num, freq in freq_dict.items(): if freq > K: res.append(num) # printing results print(""The required elements : "" + str(res)) # This code is contributed by Jyothi Pinjala."," #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]","Python - Extract elements with Frequency greater than K from collections import defaultdict # initializing list test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] # printing string print(""The original list : "" + str(test_list)) freq_dict = defaultdict(int) for num in test_list: freq_dict[num] += 1 K = 2 res = [] for num, freq in freq_dict.items(): if freq > K: res.append(num) # printing results print(""The required elements : "" + str(res)) # This code is contributed by Jyothi Pinjala. #Output : The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8] [END]" Python - Test if List contains elements in Range,https://www.geeksforgeeks.org/python-test-if-list-contains-elements-in-range/,"# Python3 code to demonstrate # Test if List contains elements in Range # using loop # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using loop res = True for ele in test_list: if ele < i or ele >= j: res = False break # printing result print(""Does list contain all elements in range : "" + str(res))"," #Output : The original list is : [4, 5, 6, 7, 3, 9]","Python - Test if List contains elements in Range # Python3 code to demonstrate # Test if List contains elements in Range # using loop # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using loop res = True for ele in test_list: if ele < i or ele >= j: res = False break # printing result print(""Does list contain all elements in range : "" + str(res)) #Output : The original list is : [4, 5, 6, 7, 3, 9] [END]" Python - Test if List contains elements in Range,https://www.geeksforgeeks.org/python-test-if-list-contains-elements-in-range/,"# Python3 code to demonstrate # Test if List contains elements in Range # using all() # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using all() res = all(ele >= i and ele < j for ele in test_list) # printing result print(""Does list contain all elements in range : "" + str(res))"," #Output : The original list is : [4, 5, 6, 7, 3, 9]","Python - Test if List contains elements in Range # Python3 code to demonstrate # Test if List contains elements in Range # using all() # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using all() res = all(ele >= i and ele < j for ele in test_list) # printing result print(""Does list contain all elements in range : "" + str(res)) #Output : The original list is : [4, 5, 6, 7, 3, 9] [END]" Python - Test if List contains elements in Range,https://www.geeksforgeeks.org/python-test-if-list-contains-elements-in-range/,"# Python3 code to demonstrate # Test if List contains elements in Range # using List Comprehension and len() # Initializing list test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using List Comprehension and len() out_of_range = len([ele for ele in test_list if ele < i or ele >= j]) == 0 # printing result print(""Does list contain all elements in range : "" + str(out_of_range))"," #Output : The original list is : [4, 5, 6, 7, 3, 9]","Python - Test if List contains elements in Range # Python3 code to demonstrate # Test if List contains elements in Range # using List Comprehension and len() # Initializing list test_list = [4, 5, 6, 7, 3, 9] # printing original list print(""The original list is : "" + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using List Comprehension and len() out_of_range = len([ele for ele in test_list if ele < i or ele >= j]) == 0 # printing result print(""Does list contain all elements in range : "" + str(out_of_range)) #Output : The original list is : [4, 5, 6, 7, 3, 9] [END]" Python - Test if List contains elements in Range,https://www.geeksforgeeks.org/python-test-if-list-contains-elements-in-range/,"# Python3 code to demonstrate # Test if List contains elements in Range # using any() # Initializing list and range boundaries test_list = [4, 5, 6, 7, 3, 9] i, j = 3, 10 # Checking if any element in the list is within the range res = any(i <= x < j for x in test_list) # Printing the result print(""Does list contain any element in range: "" + str(res))"," #Output : The original list is : [4, 5, 6, 7, 3, 9]","Python - Test if List contains elements in Range # Python3 code to demonstrate # Test if List contains elements in Range # using any() # Initializing list and range boundaries test_list = [4, 5, 6, 7, 3, 9] i, j = 3, 10 # Checking if any element in the list is within the range res = any(i <= x < j for x in test_list) # Printing the result print(""Does list contain any element in range: "" + str(res)) #Output : The original list is : [4, 5, 6, 7, 3, 9] [END]" Python program to check if the list contains three consecutive common numbers in Python,https://www.geeksforgeeks.org/python-program-to-check-if-the-list-contains-three-consecutive-common-numbers-in-python/,"# creating the array arr = [4, 5, 5, 5, 3, 8] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): # checking the conditions if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]: # printing the element as the # conditions are satisfied print(arr[i])","#Input : [4, 5, 5, 5, 3, 8] #Output : 5","Python program to check if the list contains three consecutive common numbers in Python # creating the array arr = [4, 5, 5, 5, 3, 8] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): # checking the conditions if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]: # printing the element as the # conditions are satisfied print(arr[i]) #Input : [4, 5, 5, 5, 3, 8] #Output : 5 [END]" Python program to check if the list contains three consecutive common numbers in Python,https://www.geeksforgeeks.org/python-program-to-check-if-the-list-contains-three-consecutive-common-numbers-in-python/,"# creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): # checking the conditions if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]: # printing the element as the # conditions are satisfied print(arr[i])","#Input : [4, 5, 5, 5, 3, 8] #Output : 5","Python program to check if the list contains three consecutive common numbers in Python # creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): # checking the conditions if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]: # printing the element as the # conditions are satisfied print(arr[i]) #Input : [4, 5, 5, 5, 3, 8] #Output : 5 [END]" Python program to check if the list contains three consecutive common numbers in Python,https://www.geeksforgeeks.org/python-program-to-check-if-the-list-contains-three-consecutive-common-numbers-in-python/,"# creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): elements = set([arr[i], arr[i + 1], arr[i + 2]]) if len(elements) == 1: print(arr[i])","#Input : [4, 5, 5, 5, 3, 8] #Output : 5","Python program to check if the list contains three consecutive common numbers in Python # creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] # size of the list size = len(arr) # looping till length - 2 for i in range(size - 2): elements = set([arr[i], arr[i + 1], arr[i + 2]]) if len(elements) == 1: print(arr[i]) #Input : [4, 5, 5, 5, 3, 8] #Output : 5 [END]" Python program to check if the list contains three consecutive common numbers in Python,https://www.geeksforgeeks.org/python-program-to-check-if-the-list-contains-three-consecutive-common-numbers-in-python/,"import itertools # creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] triples = zip(arr, arr[1:], arr[2:]) print([x for x, y, z in itertools.islice(triples, len(arr) - 2) if x == y == z]) # This code is contributed by Jyothi pinjala","#Input : [4, 5, 5, 5, 3, 8] #Output : 5","Python program to check if the list contains three consecutive common numbers in Python import itertools # creating the array arr = [1, 1, 1, 64, 23, 64, 22, 22, 22] triples = zip(arr, arr[1:], arr[2:]) print([x for x, y, z in itertools.islice(triples, len(arr) - 2) if x == y == z]) # This code is contributed by Jyothi pinjala #Input : [4, 5, 5, 5, 3, 8] #Output : 5 [END]" Python program to find the Strongest Neighbour,https://www.geeksforgeeks.org/python-program-to-find-the-strongest-neighbour/,"# define a function for finding # the maximum for adjacent # pairs in the array def maximumAdjacent(arr1, n): # array to store the max # value between adjacent pairs arr2 = [] # iterate from 1 to n - 1 for i in range(1, n): # find max value between # adjacent pairs gets # stored in r r = max(arr1[i], arr1[i - 1]) # add element arr2.append(r) # printing the elements for ele in arr2: print(ele, end="" "") if __name__ == ""__main__"": # size of the input array n = 6 # input array arr1 = [1, 2, 2, 3, 4, 5] # function calling maximumAdjacent(arr1, n)","Input: 1 2 2 3 4 5 Output: 2 2 3 4 5","Python program to find the Strongest Neighbour # define a function for finding # the maximum for adjacent # pairs in the array def maximumAdjacent(arr1, n): # array to store the max # value between adjacent pairs arr2 = [] # iterate from 1 to n - 1 for i in range(1, n): # find max value between # adjacent pairs gets # stored in r r = max(arr1[i], arr1[i - 1]) # add element arr2.append(r) # printing the elements for ele in arr2: print(ele, end="" "") if __name__ == ""__main__"": # size of the input array n = 6 # input array arr1 = [1, 2, 2, 3, 4, 5] # function calling maximumAdjacent(arr1, n) Input: 1 2 2 3 4 5 Output: 2 2 3 4 5 [END]" Python Program to print all Possible Combinations from the three Digits,https://www.geeksforgeeks.org/python-program-to-print-all-possible-combinations-from-the-three-digits/,"# Python program to print all # the possible combinations def comb(L): for i in range(3): for j in range(3): for k in range(3): # check if the indexes are not # same if i != j and j != k and i != k: print(L[i], L[j], L[k]) # Driver Code comb([1, 2, 3])","Input: [1, 2, 3] Output: ","Python Program to print all Possible Combinations from the three Digits # Python program to print all # the possible combinations def comb(L): for i in range(3): for j in range(3): for k in range(3): # check if the indexes are not # same if i != j and j != k and i != k: print(L[i], L[j], L[k]) # Driver Code comb([1, 2, 3]) Input: [1, 2, 3] Output: [END]" Python Program to print all Possible Combinations from the three Digits,https://www.geeksforgeeks.org/python-program-to-print-all-possible-combinations-from-the-three-digits/,"# Python program to print all # the possible combinations from itertools import permutations # Get all combination of [1, 2, 3] # of length 3 comb = permutations([1, 2, 3], 3) for i in comb: print(i)","Input: [1, 2, 3] Output: ","Python Program to print all Possible Combinations from the three Digits # Python program to print all # the possible combinations from itertools import permutations # Get all combination of [1, 2, 3] # of length 3 comb = permutations([1, 2, 3], 3) for i in comb: print(i) Input: [1, 2, 3] Output: [END]" Python program to find all the Combinations in the list with the given condition,https://www.geeksforgeeks.org/python-program-to-find-all-the-combinations-in-the-list-with-the-given-condition/,"from itertools import combinations # initializing list test_list = [""GFG"", [5, 4], ""is"", [""best"", ""good"", ""better"", ""average""]] idx = 0 temp = combinations(test_list, 2) for i in list(temp): idx = idx + 1 print(""Combination"", idx, "": "", i)"," #Output : Combination 1 : ('GFG', [5, 4])","Python program to find all the Combinations in the list with the given condition from itertools import combinations # initializing list test_list = [""GFG"", [5, 4], ""is"", [""best"", ""good"", ""better"", ""average""]] idx = 0 temp = combinations(test_list, 2) for i in list(temp): idx = idx + 1 print(""Combination"", idx, "": "", i) #Output : Combination 1 : ('GFG', [5, 4]) [END]" Python program to find all the Combinations in the list with the given condition,https://www.geeksforgeeks.org/python-program-to-find-all-the-combinations-in-the-list-with-the-given-condition/,"from itertools import combinations_with_replacement # initializing list test_list = [""GFG"", [5, 4], ""is"", [""best"", ""good"", ""better"", ""average""]] idx = 0 temp = combinations_with_replacement(test_list, 2) for i in list(temp): idx = idx + 1 print(""Combination"", idx, "": "", i)"," #Output : Combination 1 : ('GFG', [5, 4])","Python program to find all the Combinations in the list with the given condition from itertools import combinations_with_replacement # initializing list test_list = [""GFG"", [5, 4], ""is"", [""best"", ""good"", ""better"", ""average""]] idx = 0 temp = combinations_with_replacement(test_list, 2) for i in list(temp): idx = idx + 1 print(""Combination"", idx, "": "", i) #Output : Combination 1 : ('GFG', [5, 4]) [END]" Python program to find all the Combinations in the list with the given condition,https://www.geeksforgeeks.org/python-program-to-find-all-the-combinations-in-the-list-with-the-given-condition/,"def combinations(iterable, r): pool = tuple(iterable) n = len(pool) if r > n: return indx = list(range(r)) yield tuple(pool[i] for i in indx) while True: for i in reversed(range(r)): if indx[i] != i + n - r: break else: return indx[i] += 1 for j in range(i + 1, r): indx[j] = indx[j - 1] + 1 yield tuple(pool[i] for i in indx) x = [2, 3, 1, 6, 4, 7] for i in combinations(x, 2): print(i)"," #Output : Combination 1 : ('GFG', [5, 4])","Python program to find all the Combinations in the list with the given condition def combinations(iterable, r): pool = tuple(iterable) n = len(pool) if r > n: return indx = list(range(r)) yield tuple(pool[i] for i in indx) while True: for i in reversed(range(r)): if indx[i] != i + n - r: break else: return indx[i] += 1 for j in range(i + 1, r): indx[j] = indx[j - 1] + 1 yield tuple(pool[i] for i in indx) x = [2, 3, 1, 6, 4, 7] for i in combinations(x, 2): print(i) #Output : Combination 1 : ('GFG', [5, 4]) [END]" Python program to find all the Combinations in the list with the given condition,https://www.geeksforgeeks.org/python-program-to-find-all-the-combinations-in-the-list-with-the-given-condition/,"import copy def combinations(target, data): for i in range(len(data)): new_lis = copy.copy(target) new_data = copy.copy(data) # print(new_lis, new_data) new_lis.append(data[i]) new_data = data[i + 1 :] print(new_lis) combinations(new_lis, new_data) target = [] data = [1, 2, 3, 4] combinations(target, data)"," #Output : Combination 1 : ('GFG', [5, 4])","Python program to find all the Combinations in the list with the given condition import copy def combinations(target, data): for i in range(len(data)): new_lis = copy.copy(target) new_data = copy.copy(data) # print(new_lis, new_data) new_lis.append(data[i]) new_data = data[i + 1 :] print(new_lis) combinations(new_lis, new_data) target = [] data = [1, 2, 3, 4] combinations(target, data) #Output : Combination 1 : ('GFG', [5, 4]) [END]" Python program to get all unique combinations of two Lists,https://www.geeksforgeeks.org/python-program-to-get-all-unique-combinations-of-two-lists/,"# python program to demonstrate # unique combination of two lists # using zip() and permutation of itertools # import itertools package import itertools from itertools import permutations # initialize lists list_1 = [""a"", ""b"", ""c"", ""d""] list_2 = [1, 4, 9] # create empty list to store the # combinations unique_combinations = [] # Getting all permutations of list_1 # with length of list_2 permut = itertools.permutations(list_1, len(list_2)) # zip() is called to pair each permutation # and shorter list element into combination for comb in permut: zipped = zip(comb, list_2) unique_combinations.append(list(zipped)) # printing unique_combination list print(unique_combinations)"," #Output : List_1 = [""a"",""b""]","Python program to get all unique combinations of two Lists # python program to demonstrate # unique combination of two lists # using zip() and permutation of itertools # import itertools package import itertools from itertools import permutations # initialize lists list_1 = [""a"", ""b"", ""c"", ""d""] list_2 = [1, 4, 9] # create empty list to store the # combinations unique_combinations = [] # Getting all permutations of list_1 # with length of list_2 permut = itertools.permutations(list_1, len(list_2)) # zip() is called to pair each permutation # and shorter list element into combination for comb in permut: zipped = zip(comb, list_2) unique_combinations.append(list(zipped)) # printing unique_combination list print(unique_combinations) #Output : List_1 = [""a"",""b""] [END]" Python program to get all unique combinations of two Lists,https://www.geeksforgeeks.org/python-program-to-get-all-unique-combinations-of-two-lists/,"# python program to demonstrate # unique combination of two lists # using zip() and product() of itertools # import itertools package import itertools from itertools import product # initialize lists list_1 = [""b"", ""c"", ""d""] list_2 = [1, 4, 9] # create empty list to store the combinations unique_combinations = [] # Extract Combination Mapping in two lists # using zip() + product() unique_combinations = list( list(zip(list_1, element)) for element in product(list_2, repeat=len(list_1)) ) # printing unique_combination list print(unique_combinations)"," #Output : List_1 = [""a"",""b""]","Python program to get all unique combinations of two Lists # python program to demonstrate # unique combination of two lists # using zip() and product() of itertools # import itertools package import itertools from itertools import product # initialize lists list_1 = [""b"", ""c"", ""d""] list_2 = [1, 4, 9] # create empty list to store the combinations unique_combinations = [] # Extract Combination Mapping in two lists # using zip() + product() unique_combinations = list( list(zip(list_1, element)) for element in product(list_2, repeat=len(list_1)) ) # printing unique_combination list print(unique_combinations) #Output : List_1 = [""a"",""b""] [END]" Python program to get all unique combinations of two Lists,https://www.geeksforgeeks.org/python-program-to-get-all-unique-combinations-of-two-lists/,"list_1 = [""b"", ""c"", ""d""] list_2 = [1, 4, 9] unique_combinations = [] for i in range(len(list_1)): for j in range(len(list_2)): unique_combinations.append((list_1[i], list_2[j])) print(unique_combinations)"," #Output : List_1 = [""a"",""b""]","Python program to get all unique combinations of two Lists list_1 = [""b"", ""c"", ""d""] list_2 = [1, 4, 9] unique_combinations = [] for i in range(len(list_1)): for j in range(len(list_2)): unique_combinations.append((list_1[i], list_2[j])) print(unique_combinations) #Output : List_1 = [""a"",""b""] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"# Python 3 code to demonstrate # the removal of all occurrences of a # given item using list comprehension def remove_items(test_list, item): # using list comprehension to perform the task res = [i for i in test_list if i != item] return res # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is : "" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is : "" + str(res))"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list # Python 3 code to demonstrate # the removal of all occurrences of a # given item using list comprehension def remove_items(test_list, item): # using list comprehension to perform the task res = [i for i in test_list if i != item] return res # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is : "" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is : "" + str(res)) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"# Python 3 code to demonstrate # the removal of all occurrences of # a given item using filter() and __ne__ def remove_items(test_list, item): # using filter() + __ne__ to perform the task res = list(filter((item).__ne__, test_list)) return res # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is : "" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is : "" + str(res))"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list # Python 3 code to demonstrate # the removal of all occurrences of # a given item using filter() and __ne__ def remove_items(test_list, item): # using filter() + __ne__ to perform the task res = list(filter((item).__ne__, test_list)) return res # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is : "" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is : "" + str(res)) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"# Python 3 code to demonstrate # the removal of all occurrences of # a given item using remove() def remove_items(test_list, item): # remove the item for all its occurrences c = test_list.count(item) for i in range(c): test_list.remove(item) return test_list # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is :"" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is :"" + str(res))"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list # Python 3 code to demonstrate # the removal of all occurrences of # a given item using remove() def remove_items(test_list, item): # remove the item for all its occurrences c = test_list.count(item) for i in range(c): test_list.remove(item) return test_list # driver code if __name__ == ""__main__"": # initializing the list test_list = [1, 3, 4, 6, 5, 1] # the item which is to be removed item = 1 # printing the original list print(""The original list is :"" + str(test_list)) # calling the function remove_items() res = remove_items(test_list, item) # printing result print(""The list after performing the remove operation is :"" + str(res)) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"test_list = [1, 3, 4, 6, 5, 1] ele = 1 x = [i for i in test_list if i != ele] print(x)"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list test_list = [1, 3, 4, 6, 5, 1] ele = 1 x = [i for i in test_list if i != ele] print(x) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"# remove all occurrences of element in list test_list = [1, 3, 4, 6, 5, 1] ele = 1 a = list(map(str, test_list)) b = "" "".join(a) b = b.replace(str(ele), """") b = b.split() x = list(map(int, b)) print(x)"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list # remove all occurrences of element in list test_list = [1, 3, 4, 6, 5, 1] ele = 1 a = list(map(str, test_list)) b = "" "".join(a) b = b.replace(str(ele), """") b = b.split() x = list(map(int, b)) print(x) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python program to remove all the occurrences of an element from a list,https://www.geeksforgeeks.org/remove-all-the-occurrences-of-an-element-from-a-list-in-python/,"test_list = [1, 3, 4, 6, 5, 1] ele = 1 x = [j for i, j in enumerate(test_list) if j != ele] print(x)"," #Output : The original list is : [1, 3, 4, 6, 5, 1]","Python program to remove all the occurrences of an element from a list test_list = [1, 3, 4, 6, 5, 1] ele = 1 x = [j for i, j in enumerate(test_list) if j != ele] print(x) #Output : The original list is : [1, 3, 4, 6, 5, 1] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"# Python3 code to demonstrate working of # Remove Consecutive K element records # Using zip() + list comprehension # initializing list test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 6 # Remove Consecutive K element records # Using zip() + list comprehension res = [idx for idx in test_list if (K, K) not in zip(idx, idx[1:])] # printing result print(""The records after removal : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records # Python3 code to demonstrate working of # Remove Consecutive K element records # Using zip() + list comprehension # initializing list test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 6 # Remove Consecutive K element records # Using zip() + list comprehension res = [idx for idx in test_list if (K, K) not in zip(idx, idx[1:])] # printing result print(""The records after removal : "" + str(res)) #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"# Python3 code to demonstrate working of # Remove Consecutive K element records # Using any() + list comprehension # initializing list test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 6 # Remove Consecutive K element records # Using any() + list comprehension res = [ idx for idx in test_list if not any(idx[j] == K and idx[j + 1] == K for j in range(len(idx) - 1)) ] # printing result print(""The records after removal : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records # Python3 code to demonstrate working of # Remove Consecutive K element records # Using any() + list comprehension # initializing list test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 6 # Remove Consecutive K element records # Using any() + list comprehension res = [ idx for idx in test_list if not any(idx[j] == K and idx[j + 1] == K for j in range(len(idx) - 1)) ] # printing result print(""The records after removal : "" + str(res)) #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 temp_list = [] for item in test_list: skip = False for i in range(len(item) - 1): if item[i] == K and item[i + 1] == K: skip = True break if not skip: temp_list.append(item) res = temp_list print(""The records after removal : "", res)"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 temp_list = [] for item in test_list: skip = False for i in range(len(item) - 1): if item[i] == K and item[i + 1] == K: skip = True break if not skip: temp_list.append(item) res = temp_list print(""The records after removal : "", res) #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 res = list(filter(lambda x: not any(i == j == K for i, j in zip(x, x[1:])), test_list)) print(""The records after removal : "", res)"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 res = list(filter(lambda x: not any(i == j == K for i, j in zip(x, x[1:])), test_list)) print(""The records after removal : "", res) #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"import itertools test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 res = list( itertools.filterfalse( lambda item: any( item[i] == K and item[i + 1] == K for i in range(len(item) - 1) ), test_list, ) ) print(""The records after removal : "", res)"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records import itertools test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 res = list( itertools.filterfalse( lambda item: any( item[i] == K and item[i + 1] == K for i in range(len(item) - 1) ), test_list, ) ) print(""The records after removal : "", res) #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Remove Consecutive K element records,https://www.geeksforgeeks.org/python-remove-consecutive-k-element-records/,"import numpy as np test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 # printing original list print(""The original list is : "" + str(test_list)) # Convert list of tuples to 2D NumPy array arr = np.array(test_list) # Check if consecutive elements in each row are equal to K mask = np.logical_not(np.any((arr[:, :-1] == K) & (arr[:, 1:] == K), axis=1)) # Filter the rows based on the mask res = arr[mask] print(""The records after removal : "", res) # This code is contributed by Vinay Pinjala"," #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)]","Python - Remove Consecutive K element records import numpy as np test_list = [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] K = 6 # printing original list print(""The original list is : "" + str(test_list)) # Convert list of tuples to 2D NumPy array arr = np.array(test_list) # Check if consecutive elements in each row are equal to K mask = np.logical_not(np.any((arr[:, :-1] == K) & (arr[:, 1:] == K), axis=1)) # Filter the rows based on the mask res = arr[mask] print(""The records after removal : "", res) # This code is contributed by Vinay Pinjala #Output : The original list is : [(4, 5, 6, 3), (5, 6, 6, 9), (1, 3, 5, 6), (6, 6, 7, 8)] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Replace index elements with elements in Other List # using list comprehension # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using list comprehension res = [test_list1[idx] for idx in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List # Python3 code to demonstrate # Replace index elements with elements in Other List # using list comprehension # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using list comprehension res = [test_list1[idx] for idx in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Replace index elements with elements in Other List # using map() + lambda # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using map() + lambda res = list(map(lambda idx: test_list1[idx], test_list2)) # printing result print(""The lists after index elements replacements is : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List # Python3 code to demonstrate # Replace index elements with elements in Other List # using map() + lambda # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using map() + lambda res = list(map(lambda idx: test_list1[idx], test_list2)) # printing result print(""The lists after index elements replacements is : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Replace index elements with elements in Other List # using numpy.take() # import numpy import numpy as np # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using numpy.take() res = np.take(test_list1, test_list2) # printing result print(""The lists after index elements replacements is : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List # Python3 code to demonstrate # Replace index elements with elements in Other List # using numpy.take() # import numpy import numpy as np # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace index elements with elements in Other List # using numpy.take() res = np.take(test_list1, test_list2) # printing result print(""The lists after index elements replacements is : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"# Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Create a dictionary to map indices to elements in test_list1 index_map = {index: element for index, element in enumerate(test_list1)} # Replace index elements with elements in Other List using a for loop res = [index_map[idx] for idx in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Create a dictionary to map indices to elements in test_list1 index_map = {index: element for index, element in enumerate(test_list1)} # Replace index elements with elements in Other List using a for loop res = [index_map[idx] for idx in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"import pandas as pd # Initializing the two lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # Creating a dataframe with one column ('a') containing the values from test_list1 df = pd.DataFrame({""a"": test_list1}) # Extracting the values from test_list1 corresponding to the indices in test_list2 # by using the loc[] function of the dataframe and converting the result to a list res = df.loc[test_list2, ""a""].tolist() # Printing the resulting list print(""The lists after index elements replacements is : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List import pandas as pd # Initializing the two lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # Creating a dataframe with one column ('a') containing the values from test_list1 df = pd.DataFrame({""a"": test_list1}) # Extracting the values from test_list1 corresponding to the indices in test_list2 # by using the loc[] function of the dataframe and converting the result to a list res = df.loc[test_list2, ""a""].tolist() # Printing the resulting list print(""The lists after index elements replacements is : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python - Replace index elements with elements in Other List,https://www.geeksforgeeks.org/python-replace-index-elements-with-elements-in-other-list/?ref=leftbar-rightbar,"# Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Using zip() function res = [test_list1[i] for i in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list 1 is : ['Gfg', 'is', 'best']","Python - Replace index elements with elements in Other List # Initializing lists test_list1 = [""Gfg"", ""is"", ""best""] test_list2 = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Using zip() function res = [test_list1[i] for i in test_list2] # printing result print(""The lists after index elements replacements is : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list 1 is : ['Gfg', 'is', 'best'] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# Python3 code to demonstrate working of # Retain records with N occurrences of K # Using count() + list comprehension # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using count() + list comprehension res = [ele for ele in test_list if ele.count(K) == N] # printing result print(""Filtered tuples : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # Python3 code to demonstrate working of # Retain records with N occurrences of K # Using count() + list comprehension # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using count() + list comprehension res = [ele for ele in test_list if ele.count(K) == N] # printing result print(""Filtered tuples : "" + str(res)) #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# Python3 code to demonstrate working of # Retain records with N occurrences of K # Using list comprehension + sum() # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using list comprehension + sum() res = [ele for ele in test_list if sum(cnt == K for cnt in ele) == N] # printing result print(""Filtered tuples : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # Python3 code to demonstrate working of # Retain records with N occurrences of K # Using list comprehension + sum() # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using list comprehension + sum() res = [ele for ele in test_list if sum(cnt == K for cnt in ele) == N] # printing result print(""Filtered tuples : "" + str(res)) #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# Python3 code to demonstrate working of # Retain records with N occurrences of K # Using count() + Recursive function def retain_records(lst, k, n): # Base case: if the list is empty, return an empty list if not lst: return [] # Recursive case: if the first tuple has N occurrences of K, # append it to the result list and call the function recursively # with the rest of the list. else: if lst[0].count(k) == n: return [lst[0]] + retain_records(lst[1:], k, n) else: return retain_records(lst[1:], k, n) # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using count() +Recursive function res = retain_records(test_list, K, N) # printing result print(""Filtered tuples : "" + str(res)) # this code contributed by tvsk"," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # Python3 code to demonstrate working of # Retain records with N occurrences of K # Using count() + Recursive function def retain_records(lst, k, n): # Base case: if the list is empty, return an empty list if not lst: return [] # Recursive case: if the first tuple has N occurrences of K, # append it to the result list and call the function recursively # with the rest of the list. else: if lst[0].count(k) == n: return [lst[0]] + retain_records(lst[1:], k, n) else: return retain_records(lst[1:], k, n) # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K # Using count() +Recursive function res = retain_records(test_list, K, N) # printing result print(""Filtered tuples : "" + str(res)) # this code contributed by tvsk #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# Python3 code to demonstrate working of # Retain records with N occurrences of K # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K res = [] import operator for i in test_list: if operator.countOf(i, K) == N: res.append(i) # printing result print(""Filtered tuples : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # Python3 code to demonstrate working of # Retain records with N occurrences of K # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 4 # initializing N N = 3 # Retain records with N occurrences of K res = [] import operator for i in test_list: if operator.countOf(i, K) == N: res.append(i) # printing result print(""Filtered tuples : "" + str(res)) #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# Define a list of tuples test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # Define the value of K and N K = 4 N = 3 # printing original list print(""The original list is : "" + str(test_list)) # Use the filter() function to create a new list of tuples that only contain tuples that have exactly N occurrences of K # The lambda function checks how many times K occurs in each tuple x, and returns True if the count is equal to N # The filter() function returns an iterator, so we convert it to a list with list() res = list(filter(lambda x: x.count(K) == N, test_list)) # Print out the filtered tuples print(""Filtered tuples : "" + str(res)) # This code is contributed by Jyothi pinjala."," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # Define a list of tuples test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # Define the value of K and N K = 4 N = 3 # printing original list print(""The original list is : "" + str(test_list)) # Use the filter() function to create a new list of tuples that only contain tuples that have exactly N occurrences of K # The lambda function checks how many times K occurs in each tuple x, and returns True if the count is equal to N # The filter() function returns an iterator, so we convert it to a list with list() res = list(filter(lambda x: x.count(K) == N, test_list)) # Print out the filtered tuples print(""Filtered tuples : "" + str(res)) # This code is contributed by Jyothi pinjala. #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"# initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # initializing K K = 4 # initializing N N = 3 # Using for loop res = [] for tup in test_list: if tup.count(K) == N: res.append(tup) # printing result print(""Filtered tuples : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # initializing K K = 4 # initializing N N = 3 # Using for loop res = [] for tup in test_list: if tup.count(K) == N: res.append(tup) # printing result print(""Filtered tuples : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python Program to Retain records with N occurrences of K,https://www.geeksforgeeks.org/python-retain-records-with-n-occurrences-of-k/,"import functools # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # initializing K K = 4 # initializing N N = 3 # define the function to count the number of times k occurs in a tuple def count_k_in_tuple(k, tuple_t): count = tuple_t.count(k) if count == N: return True else: return False # create a partial function using the count_k_in_tuple function and the value of K filter_func = functools.partial(count_k_in_tuple, K) # use the filter function to filter the tuples based on the given conditions filtered_tuples = filter(filter_func, test_list) # convert the filtered tuples into a list res = list(filtered_tuples) # print the result print(""Filtered tuples : "" + str(res))"," #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]","Python Program to Retain records with N occurrences of K import functools # initializing list test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] # initializing K K = 4 # initializing N N = 3 # define the function to count the number of times k occurs in a tuple def count_k_in_tuple(k, tuple_t): count = tuple_t.count(k) if count == N: return True else: return False # create a partial function using the count_k_in_tuple function and the value of K filter_func = functools.partial(count_k_in_tuple, K) # use the filter function to filter the tuples based on the given conditions filtered_tuples = filter(filter_func, test_list) # convert the filtered tuples into a list res = list(filtered_tuples) # print the result print(""Filtered tuples : "" + str(res)) #Output : The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using replace() + list comprehension # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + list comprehension res = [sub.replace(""G"", ""-"").replace(""e"", ""G"").replace(""-"", ""e"") for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using replace() + list comprehension # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + list comprehension res = [sub.replace(""G"", ""-"").replace(""e"", ""G"").replace(""-"", ""e"") for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using replace() + join() + split() # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + join() + split() res = "", "".join(test_list) res = res.replace(""G"", ""_"").replace(""e"", ""G"").replace(""_"", ""e"").split("", "") # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using replace() + join() + split() # Initializing list test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using replace() + join() + split() res = "", "".join(test_list) res = res.replace(""G"", ""_"").replace(""e"", ""G"").replace(""_"", ""e"").split("", "") # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python - Swap elements in String List,https://www.geeksforgeeks.org/python-swap-elements-in-string-list/,"# Python3 code to demonstrate # Swap elements in String list # using regex # Initializing list import re test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using regex res = [re.sub(""-"", ""e"", re.sub(""e"", ""G"", re.sub(""G"", ""-"", sub))) for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res))"," #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']","Python - Swap elements in String List # Python3 code to demonstrate # Swap elements in String list # using regex # Initializing list import re test_list = [""Gfg"", ""is"", ""best"", ""for"", ""Geeks""] # printing original lists print(""The original list is : "" + str(test_list)) # Swap elements in String list # using regex res = [re.sub(""-"", ""e"", re.sub(""e"", ""G"", re.sub(""G"", ""-"", sub))) for sub in test_list] # printing result print(""List after performing character swaps : "" + str(res)) #Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] [END]" Python program to reverse All String in String List,https://www.geeksforgeeks.org/python-reverse-all-strings-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Reverse All Strings in String List # using list comprehension # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # using list comprehension # Reverse All Strings in String List res = [i[::-1] for i in test_list] # printing result print(""The reversed string list is : "" + str(res))"," #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best']","Python program to reverse All String in String List # Python3 code to demonstrate # Reverse All Strings in String List # using list comprehension # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # using list comprehension # Reverse All Strings in String List res = [i[::-1] for i in test_list] # printing result print(""The reversed string list is : "" + str(res)) #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best'] [END]" Python program to reverse All String in String List,https://www.geeksforgeeks.org/python-reverse-all-strings-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Reverse All Strings in String List # using map() # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Reverse All Strings in String List # using map() reverse = lambda i: i[::-1] res = list(map(reverse, test_list)) # printing result print(""The reversed string list is : "" + str(res))"," #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best']","Python program to reverse All String in String List # Python3 code to demonstrate # Reverse All Strings in String List # using map() # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Reverse All Strings in String List # using map() reverse = lambda i: i[::-1] res = list(map(reverse, test_list)) # printing result print(""The reversed string list is : "" + str(res)) #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best'] [END]" Python program to reverse All String in String List,https://www.geeksforgeeks.org/python-reverse-all-strings-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Reverse All Strings in String List # using join() and reversed() # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # using join() and reversed() res = ["""".join(reversed(i)) for i in test_list] # printing result print(""The reversed string list is : "" + str(res))"," #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best']","Python program to reverse All String in String List # Python3 code to demonstrate # Reverse All Strings in String List # using join() and reversed() # initializing list test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # using join() and reversed() res = ["""".join(reversed(i)) for i in test_list] # printing result print(""The reversed string list is : "" + str(res)) #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best'] [END]" Python program to reverse All String in String List,https://www.geeksforgeeks.org/python-reverse-all-strings-in-string-list/?ref=leftbar-rightbar,"# define the original list of strings test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # create an empty list to store the reversed strings res = [] # iterate over each string in the original list for string in test_list: # use slicing to reverse the string and append it to the result list res.append(string[::-1]) # print the list of reversed strings print(res)"," #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best']","Python program to reverse All String in String List # define the original list of strings test_list = [""geeks"", ""for"", ""geeks"", ""is"", ""best""] # create an empty list to store the reversed strings res = [] # iterate over each string in the original list for string in test_list: # use slicing to reverse the string and append it to the result list res.append(string[::-1]) # print the list of reversed strings print(res) #Output : The original list is : ['geeks', 'for', 'geeks', 'is', 'best'] [END]" Python program to find the character position of Kth word from a list of strings,https://www.geeksforgeeks.org/python-program-to-find-the-character-position-of-kth-word-from-a-list-of-strings/,"# Python3 code to demonstrate working of # Word Index for K position in Strings List # Using enumerate() + list comprehension # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # enumerate to get indices of all inner and outer list res = [ele[0] for sub in enumerate(test_list) for ele in enumerate(sub[1])] # getting index of word res = res[K] # printing result print(""Index of character at Kth position word : "" + str(res))"," #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']","Python program to find the character position of Kth word from a list of strings # Python3 code to demonstrate working of # Word Index for K position in Strings List # Using enumerate() + list comprehension # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # enumerate to get indices of all inner and outer list res = [ele[0] for sub in enumerate(test_list) for ele in enumerate(sub[1])] # getting index of word res = res[K] # printing result print(""Index of character at Kth position word : "" + str(res)) #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks'] [END]" Python program to find the character position of Kth word from a list of strings,https://www.geeksforgeeks.org/python-program-to-find-the-character-position-of-kth-word-from-a-list-of-strings/,"# Python3 code to demonstrate working of # Word Index for K position in Strings List # Using next() + zip() + count() from itertools import count # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # count() for getting count # pairing using zip() cnt = count() res = next(j for sub in test_list for j, idx in zip(range(len(sub)), cnt) if idx == K) # printing result print(""Index of character at Kth position word : "" + str(res))"," #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']","Python program to find the character position of Kth word from a list of strings # Python3 code to demonstrate working of # Word Index for K position in Strings List # Using next() + zip() + count() from itertools import count # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # count() for getting count # pairing using zip() cnt = count() res = next(j for sub in test_list for j, idx in zip(range(len(sub)), cnt) if idx == K) # printing result print(""Index of character at Kth position word : "" + str(res)) #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks'] [END]" Python program to find the character position of Kth word from a list of strings,https://www.geeksforgeeks.org/python-program-to-find-the-character-position-of-kth-word-from-a-list-of-strings/,"# Python3 code to demonstrate working of # Word Index for K position in Strings List # Using nested loop # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # initializing index counter idx = 0 # iterating over each word in the list for word in test_list: # if the kth position is in the current word if idx + len(word) > K: # printing result print(""Index of character at Kth position word : "" + str(K - idx)) break # if the kth position is not in the current word else: idx += len(word) # if the kth position is beyond the end of the list else: print(""K is beyond the end of the list"")"," #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']","Python program to find the character position of Kth word from a list of strings # Python3 code to demonstrate working of # Word Index for K position in Strings List # Using nested loop # initializing list test_list = [""geekforgeeks"", ""is"", ""best"", ""for"", ""geeks""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = 20 # initializing index counter idx = 0 # iterating over each word in the list for word in test_list: # if the kth position is in the current word if idx + len(word) > K: # printing result print(""Index of character at Kth position word : "" + str(K - idx)) break # if the kth position is not in the current word else: idx += len(word) # if the kth position is beyond the end of the list else: print(""K is beyond the end of the list"") #Output : The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List # Using loop + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for sub in test_list: # splitting phrases temp = sub.split() for ele in temp: # checking for matching elements if ele[0].lower() == K.lower(): res.append(ele) # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List # Using loop + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for sub in test_list: # splitting phrases temp = sub.split() for ele in temp: # checking for matching elements if ele[0].lower() == K.lower(): res.append(ele) # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List # Using list comprehension + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [ele for temp in test_list for ele in temp.split() if ele[0].lower() == K.lower()] # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List # Using list comprehension + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [ele for temp in test_list for ele in temp.split() if ele[0].lower() == K.lower()] # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] x = [] for i in test_list: x.extend(i.split()) for j in x: if j.lower().find(K) == 0: res.append(j) # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] x = [] for i in test_list: x.extend(i.split()) for j in x: if j.lower().find(K) == 0: res.append(j) # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List # Using loop + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] x = [] for i in test_list: x.extend(i.split()) for i in x: if i.startswith(K) or i.startswith(K.lower()) or i.startswith(K.upper()): res.append(i) # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List # Using loop + split() # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] x = [] for i in test_list: x.extend(i.split()) for i in x: if i.startswith(K) or i.startswith(K.lower()) or i.startswith(K.upper()): res.append(i) # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for i in test_list: words = i.split() startWords = list(filter(lambda x: x[0].lower() == K.lower(), words)) res.extend(startWords) # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for i in test_list: words = i.split() startWords = list(filter(lambda x: x[0].lower() == K.lower(), words)) res.extend(startWords) # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# Python3 code to demonstrate working of # Extract words starting with K in String List import itertools # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for i in test_list: words = i.split() startWords = list(itertools.filterfalse(lambda x: x[0].lower() != K.lower(), words)) res.extend(startWords) # printing result print(""The filtered elements : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # Python3 code to demonstrate working of # Extract words starting with K in String List import itertools # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" res = [] for i in test_list: words = i.split() startWords = list(itertools.filterfalse(lambda x: x[0].lower() != K.lower(), words)) res.extend(startWords) # printing result print(""The filtered elements : "" + str(res)) #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Extract words starting with K in String List,https://www.geeksforgeeks.org/python-extract-words-starting-with-k-in-string-list/,"# importing reduce module from functools import reduce # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" # Extract words starting with K in String List # Using reduce() + split() res = reduce( lambda x, y: x + [ele for ele in y.split() if ele[0].lower() == K.lower()], test_list, [], ) # printing result print(""The filtered elements : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G']","Python - Extract words starting with K in String List # importing reduce module from functools import reduce # initializing list test_list = [""Gfg is best"", ""Gfg is for geeks"", ""I love G4G""] # printing original list print(""The original list is : "" + str(test_list)) # initializing K K = ""g"" # Extract words starting with K in String List # Using reduce() + split() res = reduce( lambda x, y: x + [ele for ele in y.split() if ele[0].lower() == K.lower()], test_list, [], ) # printing result print(""The filtered elements : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list is : ['Gfg is best', 'Gfg is for geeks', 'I love G4G'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Prefix frequency in List # using loop + startswith() # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using loop + startswith() res = 0 for ele in test_list: if ele.startswith(test_sub): res = res + 1 # printing result print(""Strings count with matching frequency : "" + str(res))"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list # Python3 code to demonstrate # Prefix frequency in List # using loop + startswith() # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using loop + startswith() res = 0 for ele in test_list: if ele.startswith(test_sub): res = res + 1 # printing result print(""Strings count with matching frequency : "" + str(res)) #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Prefix frequency in List # using sum() + startswith() # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using sum() + startswith() res = sum(sub.startswith(test_sub) for sub in test_list) # printing result print(""Strings count with matching frequency : "" + str(res))"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list # Python3 code to demonstrate # Prefix frequency in List # using sum() + startswith() # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using sum() + startswith() res = sum(sub.startswith(test_sub) for sub in test_list) # printing result print(""Strings count with matching frequency : "" + str(res)) #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Prefix frequency in List # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using loop + find() res = 0 for ele in test_list: if ele.find(test_sub) == 0: res = res + 1 # printing result print(""Strings count with matching frequency : "" + str(res))"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list # Python3 code to demonstrate # Prefix frequency in List # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List # using loop + find() res = 0 for ele in test_list: if ele.find(test_sub) == 0: res = res + 1 # printing result print(""Strings count with matching frequency : "" + str(res)) #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"import re # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List using re.match res = 0 for ele in test_list: if re.match(f""^{test_sub}"", ele): res += 1 # printing result print(""Strings count with matching frequency : "" + str(res)) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list import re # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # printing original list print(""The original list is : "" + str(test_list)) # Initializing substring test_sub = ""gfg"" # Prefix frequency in List using re.match res = 0 for ele in test_list: if re.match(f""^{test_sub}"", ele): res += 1 # printing result print(""Strings count with matching frequency : "" + str(res)) # This code is contributed by Edula Vinay Kumar Reddy #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"from functools import reduce # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # Initializing substring test_sub = ""gfg"" # using lambda function to check if a string starts with the given substring count_func = lambda count, string: count + 1 if string.startswith(test_sub) else count # using reduce() function to apply the lambda function to each element of the list res = reduce(count_func, test_list, 0) # printing original list print(""The original list is : "" + str(test_list)) # printing result print(""Strings count with matching frequency : "" + str(res))"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list from functools import reduce # Initializing list test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # Initializing substring test_sub = ""gfg"" # using lambda function to check if a string starts with the given substring count_func = lambda count, string: count + 1 if string.startswith(test_sub) else count # using reduce() function to apply the lambda function to each element of the list res = reduce(count_func, test_list, 0) # printing original list print(""The original list is : "" + str(test_list)) # printing result print(""Strings count with matching frequency : "" + str(res)) #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Prefix frequency in string list,https://www.geeksforgeeks.org/python-prefix-frequency-in-string-list/?ref=leftbar-rightbar,"# Initialize a list of strings test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # Initialize a substring to find in the list test_sub = ""gfg"" # Create a new iterator that contains only the elements in the list that start with the substring new_iter = filter(lambda element: element.startswith(test_sub), test_list) # Count the number of elements in the iterator using the len() method count = len(list(new_iter)) # Print the original list of strings print(""The original list is : "" + str(test_list)) # Print the number of strings in the list that start with the substring print(""Strings count with matching frequency: "", count)"," #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']","Python - Prefix frequency in string list # Initialize a list of strings test_list = [""gfgisbest"", ""geeks"", ""gfgfreak"", ""gfgCS"", ""Gcourses""] # Initialize a substring to find in the list test_sub = ""gfg"" # Create a new iterator that contains only the elements in the list that start with the substring new_iter = filter(lambda element: element.startswith(test_sub), test_list) # Count the number of elements in the iterator using the len() method count = len(list(new_iter)) # Print the original list of strings print(""The original list is : "" + str(test_list)) # Print the number of strings in the list that start with the substring print(""Strings count with matching frequency: "", count) #Output : The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Split String of list on K character # using loop + split() # Initializing list test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] # printing original list print(""The original list is : "" + str(test_list)) K = "" "" # Split String of list on K character # using loop + split() res = [] for ele in test_list: sub = ele.split(K) res.extend(sub) # printing result print(""The extended list after split strings : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character # Python3 code to demonstrate # Split String of list on K character # using loop + split() # Initializing list test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] # printing original list print(""The original list is : "" + str(test_list)) K = "" "" # Split String of list on K character # using loop + split() res = [] for ele in test_list: sub = ele.split(K) res.extend(sub) # printing result print(""The extended list after split strings : "" + str(res)) #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Split String of list on K character # using join() + split() # Initializing list test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] # printing original list print(""The original list is : "" + str(test_list)) K = "" "" # Split String of list on K character # using join() + split() res = K.join(test_list).split(K) # printing result print(""The extended list after split strings : "" + str(res))"," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character # Python3 code to demonstrate # Split String of list on K character # using join() + split() # Initializing list test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] # printing original list print(""The original list is : "" + str(test_list)) K = "" "" # Split String of list on K character # using join() + split() res = K.join(test_list).split(K) # printing result print(""The extended list after split strings : "" + str(res)) #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"import itertools test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] K = "" "" # Split String of list on K character using itertools.chain and str.split result = list(itertools.chain(*(s.split(K) for s in test_list))) print(""The extended list after split strings:"", result) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character import itertools test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] K = "" "" # Split String of list on K character using itertools.chain and str.split result = list(itertools.chain(*(s.split(K) for s in test_list))) print(""The extended list after split strings:"", result) # This code is contributed by Edula Vinay Kumar Reddy #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] K = "" "" # Split String of list on K character using List Comprehension result = [word for phrase in test_list for word in phrase.split(K)] print(""The extended list after split strings:"", result)"," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] K = "" "" # Split String of list on K character using List Comprehension result = [word for phrase in test_list for word in phrase.split(K)] print(""The extended list after split strings:"", result) #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"def split_list_on_char(test_list, char): result = [] for item in test_list: if isinstance(item, str): subitems = split_string_on_char(item, char) result.extend(subitems) else: result.append(item) return result def split_string_on_char(string, char): if char not in string: return [string] index = string.index(char) left = string[:index] right = string[index + 1 :] return [left] + split_string_on_char(right, char) test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] result = split_list_on_char(test_list, "" "") print(""The extended list after split strings:"", result)"," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character def split_list_on_char(test_list, char): result = [] for item in test_list: if isinstance(item, str): subitems = split_string_on_char(item, char) result.extend(subitems) else: result.append(item) return result def split_string_on_char(string, char): if char not in string: return [string] index = string.index(char) left = string[:index] right = string[index + 1 :] return [left] + split_string_on_char(right, char) test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] result = split_list_on_char(test_list, "" "") print(""The extended list after split strings:"", result) #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split string of list on K character,https://www.geeksforgeeks.org/python-split-string-of-list-on-k-character/?ref=leftbar-rightbar,"from functools import reduce def split_list_on_char(test_list, char): return reduce( lambda acc, item: acc + split_string_on_char(item, char) if isinstance(item, str) else acc + [item], test_list, [], ) def split_string_on_char(string, char): if char not in string: return [string] index = string.index(char) left = string[:index] right = string[index + 1 :] return [left] + split_string_on_char(right, char) test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] result = split_list_on_char(test_list, "" "") print(""The extended list after split strings:"", result) # This code is contributed by Jyothi pinjala."," #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing']","Python - Split string of list on K character from functools import reduce def split_list_on_char(test_list, char): return reduce( lambda acc, item: acc + split_string_on_char(item, char) if isinstance(item, str) else acc + [item], test_list, [], ) def split_string_on_char(string, char): if char not in string: return [string] index = string.index(char) left = string[:index] right = string[index + 1 :] return [left] + split_string_on_char(right, char) test_list = [""Gfg is best"", ""for Geeks"", ""Preparing""] result = split_list_on_char(test_list, "" "") print(""The extended list after split strings:"", result) # This code is contributed by Jyothi pinjala. #Output : The original list is : ['Gfg is best', 'for Geeks', 'Preparing'] [END]" Python - Split String on Prefix Occurrence,https://www.geeksforgeeks.org/python-split-strings-on-prefix-occurrence/,"# Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + startswith() # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res = [] for val in test_list: # checking for prefix if val.startswith(pref): # if pref found, start new list res.append([val]) else: # else append in current list res[-1].append(val) # printing result print(""Prefix Split List : "" + str(res))"," #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']","Python - Split String on Prefix Occurrence # Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + startswith() # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res = [] for val in test_list: # checking for prefix if val.startswith(pref): # if pref found, start new list res.append([val]) else: # else append in current list res[-1].append(val) # printing result print(""Prefix Split List : "" + str(res)) #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS'] [END]" Python - Split String on Prefix Occurrence,https://www.geeksforgeeks.org/python-split-strings-on-prefix-occurrence/,"# Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + zip_longest() + startswith() from itertools import zip_longest # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res, temp = [], [] for x, y in zip_longest(test_list, test_list[1:]): temp.append(x) # if prefix is found, split and start new list if y and y.startswith(pref): res.append(temp) temp = [] res.append(temp) # printing result print(""Prefix Split List : "" + str(res))"," #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']","Python - Split String on Prefix Occurrence # Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + zip_longest() + startswith() from itertools import zip_longest # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res, temp = [], [] for x, y in zip_longest(test_list, test_list[1:]): temp.append(x) # if prefix is found, split and start new list if y and y.startswith(pref): res.append(temp) temp = [] res.append(temp) # printing result print(""Prefix Split List : "" + str(res)) #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS'] [END]" Python - Split String on Prefix Occurrence,https://www.geeksforgeeks.org/python-split-strings-on-prefix-occurrence/,"def split_list_at_prefix(test_list, pref): # Initialize an empty list to hold the sublists. result = [] # Initialize an empty sublist to hold the strings that come before the prefix. sublist = [] # Iterate over the input list of strings. for string in test_list: # For each string, check if it starts with the given prefix. if string.startswith(pref): # If the string starts with the prefix, add the current sublist to the result list, # and create a new empty sublist. if sublist: result.append(sublist) sublist = [] # If the string does not start with the prefix, add it to the current sublist. sublist.append(string) # If the loop is complete, add the final sublist to the result list. if sublist: result.append(sublist) # Return the result list. return result test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and""] prefix = ""geek"" result = split_list_at_prefix(test_list, prefix) print(result)"," #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']","Python - Split String on Prefix Occurrence def split_list_at_prefix(test_list, pref): # Initialize an empty list to hold the sublists. result = [] # Initialize an empty sublist to hold the strings that come before the prefix. sublist = [] # Iterate over the input list of strings. for string in test_list: # For each string, check if it starts with the given prefix. if string.startswith(pref): # If the string starts with the prefix, add the current sublist to the result list, # and create a new empty sublist. if sublist: result.append(sublist) sublist = [] # If the string does not start with the prefix, add it to the current sublist. sublist.append(string) # If the loop is complete, add the final sublist to the result list. if sublist: result.append(sublist) # Return the result list. return result test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and""] prefix = ""geek"" result = split_list_at_prefix(test_list, prefix) print(result) #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS'] [END]" Python - Split String on Prefix Occurrence,https://www.geeksforgeeks.org/python-split-strings-on-prefix-occurrence/,"# Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + find() # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res = [] for val in test_list: # checking for prefix if val.find(pref) == 0: # if pref found, start new list res.append([val]) else: # else append in current list res[-1].append(val) # printing result print(""Prefix Split List : "" + str(res))"," #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']","Python - Split String on Prefix Occurrence # Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using loop + find() # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" res = [] for val in test_list: # checking for prefix if val.find(pref) == 0: # if pref found, start new list res.append([val]) else: # else append in current list res[-1].append(val) # printing result print(""Prefix Split List : "" + str(res)) #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS'] [END]" Python - Split String on Prefix Occurrence,https://www.geeksforgeeks.org/python-split-strings-on-prefix-occurrence/,"# Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using itertools.groupby() from itertools import groupby # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" # Using groupby() + startswith() res = [] for k, g in groupby(test_list, lambda x: x.startswith(pref)): if k: res.append(list(g)) else: if res: res[-1].extend(list(g)) # printing result print(""Prefix Split List : "" + str(res)) # This code is contributed by Jyothi Pinjala."," #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']","Python - Split String on Prefix Occurrence # Python3 code to demonstrate working of # Split Strings on Prefix Occurrence # Using itertools.groupby() from itertools import groupby # initializing list test_list = [""geeksforgeeks"", ""best"", ""geeks"", ""and"", ""geeks"", ""love"", ""CS""] # printing original list print(""The original list is : "" + str(test_list)) # initializing prefix pref = ""geek"" # Using groupby() + startswith() res = [] for k, g in groupby(test_list, lambda x: x.startswith(pref)): if k: res.append(list(g)) else: if res: res[-1].extend(list(g)) # printing result print(""Prefix Split List : "" + str(res)) # This code is contributed by Jyothi Pinjala. #Output : The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS'] [END]" Python program to Replace all Characters of a List Except the given character,https://www.geeksforgeeks.org/python-program-to-replace-all-characters-of-a-list-except-the-given-character/,"# Python3 code to demonstrate working of # Replace all Characters Except K # Using list comprehension and conditional expressions # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # list comprehension to remake list after replacement res = [ele if ele == ret_chr else repl_chr for ele in test_list] # printing result print(""List after replacement : "" + str(res))"," #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']","Python program to Replace all Characters of a List Except the given character # Python3 code to demonstrate working of # Replace all Characters Except K # Using list comprehension and conditional expressions # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # list comprehension to remake list after replacement res = [ele if ele == ret_chr else repl_chr for ele in test_list] # printing result print(""List after replacement : "" + str(res)) #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T'] [END]" Python program to Replace all Characters of a List Except the given character,https://www.geeksforgeeks.org/python-program-to-replace-all-characters-of-a-list-except-the-given-character/,"# Python3 code to demonstrate working of # Replace all Characters Except K # Using map() + lambda # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # using map() to extend logic to each element of list res = list(map(lambda ele: ret_chr if ele == ret_chr else repl_chr, test_list)) # printing result print(""List after replacement : "" + str(res))"," #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']","Python program to Replace all Characters of a List Except the given character # Python3 code to demonstrate working of # Replace all Characters Except K # Using map() + lambda # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # using map() to extend logic to each element of list res = list(map(lambda ele: ret_chr if ele == ret_chr else repl_chr, test_list)) # printing result print(""List after replacement : "" + str(res)) #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T'] [END]" Python program to Replace all Characters of a List Except the given character,https://www.geeksforgeeks.org/python-program-to-replace-all-characters-of-a-list-except-the-given-character/,"# Python3 code to demonstrate working of # Replace all Characters Except K # Using for loop # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # creating an empty list to store the modified characters res = [] # iterating through the list and replacing the characters for ele in test_list: if ele == ret_chr: res.append(ele) else: res.append(repl_chr) # printing result print(""List after replacement : "" + str(res))"," #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']","Python program to Replace all Characters of a List Except the given character # Python3 code to demonstrate working of # Replace all Characters Except K # Using for loop # initializing lists test_list = [""G"", ""F"", ""G"", ""I"", ""S"", ""B"", ""E"", ""S"", ""T""] # printing original list print(""The original list : "" + str(test_list)) # initializing repl_chr repl_chr = ""$"" # initializing retain chararter ret_chr = ""G"" # creating an empty list to store the modified characters res = [] # iterating through the list and replacing the characters for ele in test_list: if ele == ret_chr: res.append(ele) else: res.append(repl_chr) # printing result print(""List after replacement : "" + str(res)) #Output : The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters # using list comprehension + all() from itertools import groupby # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using list comprehension + all() res = [ele for ele in test_list if all(ch not in ele for ch in char_list)] # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters # using list comprehension + all() from itertools import groupby # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using list comprehension + all() res = [ele for ele in test_list if all(ch not in ele for ch in char_list)] # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters # using loop from itertools import groupby # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using loop res = [] flag = 1 for ele in test_list: for idx in char_list: if idx not in ele: flag = 1 else: flag = 0 break if flag == 1: res.append(ele) # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters # using loop from itertools import groupby # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using loop res = [] flag = 1 for ele in test_list: for idx in char_list: if idx not in ele: flag = 1 else: flag = 0 break if flag == 1: res.append(ele) # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = [] for i in test_list: x = i for j in char_list: i = i.replace(j, """") if len(i) == len(x): res.append(i) # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = [] for i in test_list: x = i for j in char_list: i = i.replace(j, """") if len(i) == len(x): res.append(i) # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters from collections import Counter test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = [] freqCharList = Counter(char_list) for i in test_list: unique = True for char in i: if char in freqCharList.keys(): unique = False break if unique: res.append(i) # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters from collections import Counter test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = [] freqCharList = Counter(char_list) for i in test_list: unique = True for char in i: if char in freqCharList.keys(): unique = False break if unique: res.append(i) # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters from itertools import groupby import operator as op # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using loop res = [] flag = 1 for ele in test_list: for idx in char_list: if op.countOf(ele, idx) == 0: flag = 1 else: flag = 0 break if flag == 1: res.append(ele) # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters from itertools import groupby import operator as op # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using loop res = [] flag = 1 for ele in test_list: for idx in char_list: if op.countOf(ele, idx) == 0: flag = 1 else: flag = 0 break if flag == 1: res.append(ele) # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters def remove_words(start, lst, charlst, newlist=[]): if start == len(lst): return newlist flag = 0 for i in charlst: if i in lst[start]: flag = 1 if flag == 0: newlist.append(lst[start]) return remove_words(start + 1, lst, charlst, newlist) # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using recursive function res = remove_words(0, test_list, char_list) # printing result print(""The filtered strings are : "" + str(res)) # this code contributed by tvsk"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters def remove_words(start, lst, charlst, newlist=[]): if start == len(lst): return newlist flag = 0 for i in charlst: if i in lst[start]: flag = 1 if flag == 0: newlist.append(lst[start]) return remove_words(start + 1, lst, charlst, newlist) # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters # using recursive function res = remove_words(0, test_list, char_list) # printing result print(""The filtered strings are : "" + str(res)) # this code contributed by tvsk #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) res = list(filter(lambda x: not any(y in x for y in char_list), test_list)) print(res)"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] char_list = [""g"", ""o""] # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) res = list(filter(lambda x: not any(y in x for y in char_list), test_list)) print(res) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Remove words containing list characters,https://www.geeksforgeeks.org/python-remove-words-containing-list-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate # Remove words containing list characters # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = set([""g"", ""o""]) # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = list(filter(lambda x: not set(x).intersection(char_list), test_list)) # printing result print(""The filtered strings are : "" + str(res))"," #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks']","Python - Remove words containing list characters # Python3 code to demonstrate # Remove words containing list characters # initializing list test_list = [""gfg"", ""is"", ""best"", ""for"", ""geeks""] # initializing char list char_list = set([""g"", ""o""]) # printing original list print(""The original list is : "" + str(test_list)) # printing character list print(""The character list is : "" + str(char_list)) # Remove words containing list characters res = list(filter(lambda x: not set(x).intersection(char_list), test_list)) # printing result print(""The filtered strings are : "" + str(res)) #Output : The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using loop + strip() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using loop + strip() res = [] for ele in test_list: if ele.strip(): res.append(ele) # printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using loop + strip() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using loop + strip() res = [] for ele in test_list: if ele.strip(): res.append(ele) # printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using list comprehension + strip() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using list comprehension + strip() res = [ele for ele in test_list if ele.strip()] # printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using list comprehension + strip() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using list comprehension + strip() res = [ele for ele in test_list if ele.strip()] # printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using find() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using find() res = [] for ele in test_list: if ele.find("" "") == -1: res.append(ele) # printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Using find() # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List # Using find() res = [] for ele in test_list: if ele.find("" "") == -1: res.append(ele) # printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) res = list(filter(lambda x: x[0].lower() != x[0].upper(), test_list)) # printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) res = list(filter(lambda x: x[0].lower() != x[0].upper(), test_list)) # printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # initializing list import itertools test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) res = list(itertools.filterfalse(lambda x: x[0].upper() == x[0].lower(), test_list)) # printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # initializing list import itertools test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # printing original list print(""The original list is : "" + str(test_list)) res = list(itertools.filterfalse(lambda x: x[0].upper() == x[0].lower(), test_list)) # printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # Printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List res = [ele for ele in test_list if not ele.isspace()] # Printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # Printing original list print(""The original list is : "" + str(test_list)) # Remove multiple empty spaces from string List res = [ele for ele in test_list if not ele.isspace()] # Printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Ways to remove multiple empty spaces from string list,https://www.geeksforgeeks.org/python-ways-to-remove-multiple-empty-spaces-from-string-list/?ref=leftbar-rightbar,"import re # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # Printing original list print(""The original list is : "" + str(test_list)) string = """".join(test_list) # Remove multiple empty spaces from string List res = re.findall(r""[a-zA-Z]+"", string) # Printing result print(""List after filtering non-empty strings : "" + str(res))"," #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']","Python - Ways to remove multiple empty spaces from string list import re # Python3 code to demonstrate working of # Remove multiple empty spaces from string List # Initializing list test_list = [""gfg"", "" "", "" "", ""is"", "" "", ""best""] # Printing original list print(""The original list is : "" + str(test_list)) string = """".join(test_list) # Remove multiple empty spaces from string List res = re.findall(r""[a-zA-Z]+"", string) # Printing result print(""List after filtering non-empty strings : "" + str(res)) #Output : The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"# Python3 code to demonstrate working of # Add Space between Potential Words # Using loop + join() # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] # loop to iterate all strings for ele in test_list: temp = [[]] for char in ele: # checking for upper case character if char.isupper(): temp.append([]) # appending character at latest list temp[-1].append(char) # joining lists after adding space res.append("" "".join("""".join(ele) for ele in temp)) # printing result print(""The space added list of strings : "" + str(res))"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words # Python3 code to demonstrate working of # Add Space between Potential Words # Using loop + join() # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] # loop to iterate all strings for ele in test_list: temp = [[]] for char in ele: # checking for upper case character if char.isupper(): temp.append([]) # appending character at latest list temp[-1].append(char) # joining lists after adding space res.append("" "".join("""".join(ele) for ele in temp)) # printing result print(""The space added list of strings : "" + str(res)) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"# Python3 code to demonstrate working of # Add Space between Potential Words # Using regex() + list comprehension import re # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) # using regex() to perform task res = [re.sub(r""(\w)([A-Z])"", r""\1 \2"", ele) for ele in test_list] # printing result print(""The space added list of strings : "" + str(res))"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words # Python3 code to demonstrate working of # Add Space between Potential Words # Using regex() + list comprehension import re # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) # using regex() to perform task res = [re.sub(r""(\w)([A-Z])"", r""\1 \2"", ele) for ele in test_list] # printing result print(""The space added list of strings : "" + str(res)) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"# Python3 code to demonstrate working of # Add Space between Potential Words # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] for i in test_list: for j in i: if j.isupper(): i = i.replace(j, "" "" + j) res.append(i) # printing result print(""The space added list of strings : "" + str(res))"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words # Python3 code to demonstrate working of # Add Space between Potential Words # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] for i in test_list: for j in i: if j.isupper(): i = i.replace(j, "" "" + j) res.append(i) # printing result print(""The space added list of strings : "" + str(res)) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"# Python3 code to demonstrate working of # Add Space between Potential Words # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] alphabets = ""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" for i in test_list: s = """" for j in i: if j in alphabets: s += "" "" + j else: s += j res.append(s) # printing result print(""The space added list of strings : "" + str(res))"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words # Python3 code to demonstrate working of # Add Space between Potential Words # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) res = [] alphabets = ""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" for i in test_list: s = """" for j in i: if j in alphabets: s += "" "" + j else: s += j res.append(s) # printing result print(""The space added list of strings : "" + str(res)) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"import re def add_space(strings): return [re.sub(r""([A-Z][a-z]+)"", r"" \1"", ele) for ele in strings] test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) result = add_space(test_list) print(""The space added list of strings:"", result) # This code is contributed by Jyothi pinjala"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words import re def add_space(strings): return [re.sub(r""([A-Z][a-z]+)"", r"" \1"", ele) for ele in strings] test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) result = add_space(test_list) print(""The space added list of strings:"", result) # This code is contributed by Jyothi pinjala #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"import re # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) # define regular expression pattern pattern = r""[A-Z]+[a-z]+[^a-zA-Z]*"" # define function to add spaces using regex def add_space_regex(s): return re.sub(pattern, r"" \g<0>"", s).strip() # apply function to each element of the list using list comprehension res = [add_space_regex(s) for s in test_list] # printing result print(""The space added list of strings : "" + str(res))"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words import re # initializing list test_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] # printing original list print(""The original list : "" + str(test_list)) # define regular expression pattern pattern = r""[A-Z]+[a-z]+[^a-zA-Z]*"" # define function to add spaces using regex def add_space_regex(s): return re.sub(pattern, r"" \g<0>"", s).strip() # apply function to each element of the list using list comprehension res = [add_space_regex(s) for s in test_list] # printing result print(""The space added list of strings : "" + str(res)) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Add Space between Potential Words,https://www.geeksforgeeks.org/python-add-space-between-potential-words/,"from collections import defaultdict original_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] modified_list = [] for word in original_list: d = defaultdict(str) for i, c in enumerate(word): if c.isupper(): d[i] += "" "" d[i] += c modified_list.append("""".join(d.values())) print(""The modified list:"", modified_list)"," #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience']","Python - Add Space between Potential Words from collections import defaultdict original_list = [""gfgBest"", ""forGeeks"", ""andComputerScience""] modified_list = [] for word in original_list: d = defaultdict(str) for i, c in enumerate(word): if c.isupper(): d[i] += "" "" d[i] += c modified_list.append("""".join(d.values())) print(""The modified list:"", modified_list) #Output : The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"# Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using zip() + loop + in # operator # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" res = [] # using zip() to map by index for ele1, ele2 in zip(test_list1, test_list2): # checking for substring if sub_str in ele2: res.append(ele1) # printing result print(""The extracted list : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring # Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using zip() + loop + in # operator # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" res = [] # using zip() to map by index for ele1, ele2 in zip(test_list1, test_list2): # checking for substring if sub_str in ele2: res.append(ele1) # printing result print(""The extracted list : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"# Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using list comprehension + zip() # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""no"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using list comprehension to perform task res = [ele1 for ele1, ele2 in zip(test_list1, test_list2) if sub_str in ele2] # printing result print(""The extracted list : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring # Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using list comprehension + zip() # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""no"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using list comprehension to perform task res = [ele1 for ele1, ele2 in zip(test_list1, test_list2) if sub_str in ele2] # printing result print(""The extracted list : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"# Python3 code to demonstrate working of # Extract elements filtered by substring # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" res = [] for i in range(0, len(test_list2)): if test_list2[i].find(sub_str) != -1: res.append(test_list1[i]) # printing result print(""The extracted list : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring # Python3 code to demonstrate working of # Extract elements filtered by substring # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" res = [] for i in range(0, len(test_list2)): if test_list2[i].find(sub_str) != -1: res.append(test_list1[i]) # printing result print(""The extracted list : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"# Python3 code to demonstrate working of # Extract elements filtered by substring # from other list using filter() function # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using filter() function to filter elements res = list(filter(lambda x: sub_str in test_list2[test_list1.index(x)], test_list1)) # printing result print(""The extracted list : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring # Python3 code to demonstrate working of # Extract elements filtered by substring # from other list using filter() function # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using filter() function to filter elements res = list(filter(lambda x: sub_str in test_list2[test_list1.index(x)], test_list1)) # printing result print(""The extracted list : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"# Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using itertools.compress() import itertools # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using compress() to filter corresponding elements from test_list1 filtered_list = itertools.compress(test_list1, [sub_str in ele for ele in test_list2]) # converting filter object to list res = list(filtered_list) # printing result print(""The extracted list : "" + str(res))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring # Python3 code to demonstrate working of # Extract elements filtered by substring # from other list Using itertools.compress() import itertools # initializing list test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # initializing substr sub_str = ""ok"" # using compress() to filter corresponding elements from test_list1 filtered_list = itertools.compress(test_list1, [sub_str in ele for ele in test_list2]) # converting filter object to list res = list(filtered_list) # printing result print(""The extracted list : "" + str(res)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python - Filter the List of String whose index in second List contains the given Substring,https://www.geeksforgeeks.org/python-filter-the-list-of-string-whose-index-in-second-list-contaons-the-given-substring/,"test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] sub_str = ""ok"" res = list(filter(lambda x: x[1].find(sub_str) != -1, zip(test_list1, test_list2))) extracted_list = list(map(lambda x: x[0], res)) print(""The extracted list : "" + str(extracted_list))"," #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']","Python - Filter the List of String whose index in second List contains the given Substring test_list1 = [""Gfg"", ""is"", ""not"", ""best"", ""and"", ""not"", ""for"", ""CS""] test_list2 = [""Its ok"", ""all ok"", ""wrong"", ""looks ok"", ""ok"", ""wrong"", ""ok"", ""thats ok""] sub_str = ""ok"" res = list(filter(lambda x: x[1].find(sub_str) != -1, zip(test_list1, test_list2))) extracted_list = list(map(lambda x: x[0], res)) print(""The extracted list : "" + str(extracted_list)) #Output : The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS'] [END]" Python | Convert Character Matrix Rowix to single String,https://www.geeksforgeeks.org/python-convert-character-matrix-to-single-string/,"# Python3 code to demonstrate working of # Convert Character Matrix to single String # Using join() + list comprehension # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using join() + list comprehension res = """".join(ele for sub in test_list for ele in sub) # printing result print(""The String after join : "" + res)"," #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]","Python | Convert Character Matrix Rowix to single String # Python3 code to demonstrate working of # Convert Character Matrix to single String # Using join() + list comprehension # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using join() + list comprehension res = """".join(ele for sub in test_list for ele in sub) # printing result print(""The String after join : "" + res) #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']] [END]" Python | Convert Character Matrix Rowix to single String,https://www.geeksforgeeks.org/python-convert-character-matrix-to-single-string/,"# Python3 code to demonstrate working of # Convert Character Matrix to single String # Using join() + chain() from itertools import chain # Initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # Printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using join() + chain() res = """".join(chain(*test_list)) # Printing result print(""The String after join : "" + res)"," #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]","Python | Convert Character Matrix Rowix to single String # Python3 code to demonstrate working of # Convert Character Matrix to single String # Using join() + chain() from itertools import chain # Initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # Printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using join() + chain() res = """".join(chain(*test_list)) # Printing result print(""The String after join : "" + res) #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']] [END]" Python | Convert Character Matrix Rowix to single String,https://www.geeksforgeeks.org/python-convert-character-matrix-to-single-string/,"# Python3 code to demonstrate working of # Convert Character Matrix to single String # Using sum() + map() # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using sum() + map() res = """".join(sum(map(list, test_list), [])) # printing result print(""The String after join : "" + res)"," #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]","Python | Convert Character Matrix Rowix to single String # Python3 code to demonstrate working of # Convert Character Matrix to single String # Using sum() + map() # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using sum() + map() res = """".join(sum(map(list, test_list), [])) # printing result print(""The String after join : "" + res) #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']] [END]" Python | Convert Character Matrix Rowix to single String,https://www.geeksforgeeks.org/python-convert-character-matrix-to-single-string/,"# Python3 code to demonstrate working of # Convert Character Matrix to single String # Using reduce() from functools import reduce # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using reduce() res = reduce(lambda x, y: x + y, [char for row in test_list for char in row]) # printing result print(""The String after join : "" + res)"," #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]","Python | Convert Character Matrix Rowix to single String # Python3 code to demonstrate working of # Convert Character Matrix to single String # Using reduce() from functools import reduce # initializing list test_list = [[""g"", ""f"", ""g""], [""i"", ""s""], [""b"", ""e"", ""s"", ""t""]] # printing original list print(""The original list is : "" + str(test_list)) # Convert Character Matrix to single String # Using reduce() res = reduce(lambda x, y: x + y, [char for row in test_list for char in row]) # printing result print(""The String after join : "" + res) #Output : The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # using list comprehension + len() # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # using list comprehension + len() # Count Strings with substring String List res = len([i for i in test_list if subs in i]) # printing result print(""All strings count with given substring are : "" + str(res))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # using list comprehension + len() # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # using list comprehension + len() # Count Strings with substring String List res = len([i for i in test_list if subs in i]) # printing result print(""All strings count with given substring are : "" + str(res)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # using filter() + lambda + len() # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # using filter() + lambda + len() # Count Strings with substring String List res = len(list(filter(lambda x: subs in x, test_list))) # printing result print(""All strings count with given substring are : "" + str(res))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # using filter() + lambda + len() # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # using filter() + lambda + len() # Count Strings with substring String List res = len(list(filter(lambda x: subs in x, test_list))) # printing result print(""All strings count with given substring are : "" + str(res)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = 0 for i in test_list: if i.find(subs) != -1: res += 1 # printing result print(""All strings count with given substring are : "" + str(res))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = 0 for i in test_list: if i.find(subs) != -1: res += 1 # printing result print(""All strings count with given substring are : "" + str(res)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = 0 import operator for i in test_list: if operator.contains(i, subs): res += 1 # printing result print(""All strings count with given substring are : "" + str(res))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = 0 import operator for i in test_list: if operator.contains(i, subs): res += 1 # printing result print(""All strings count with given substring are : "" + str(res)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = sum(1 for s in test_list if s.count(subs) > 0) # printing result print(""All strings count with given substring are : "" + str(res))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is : "" + str(test_list)) # initializing substring subs = ""Geek"" # Count Strings with substring String List res = sum(1 for s in test_list if s.count(subs) > 0) # printing result print(""All strings count with given substring are : "" + str(res)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"from functools import reduce test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] subs = ""Geek"" # printing original list print(""The original list is : "" + str(test_list)) count = reduce(lambda x, y: x + 1 if subs in y else x, test_list, 0) # printing result print(""All strings count with given substring are :"", count) # This code is contributed by Jyothi pinjala."," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List from functools import reduce test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] subs = ""Geek"" # printing original list print(""The original list is : "" + str(test_list)) count = reduce(lambda x, y: x + 1 if subs in y else x, test_list, 0) # printing result print(""All strings count with given substring are :"", count) # This code is contributed by Jyothi pinjala. #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Count String Lists with substring String List,https://www.geeksforgeeks.org/python-count-strings-with-substring-string-list/?ref=leftbar-rightbar,"# Python code to demonstrate # Count Strings with substring String List # using for loop # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is: "" + str(test_list)) # initializing substring subs = ""Geek"" # using for loop count = 0 for string in test_list: if subs in string: count += 1 # printing result print(""All strings count with given substring are: "" + str(count))"," #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']","Python - Count String Lists with substring String List # Python code to demonstrate # Count Strings with substring String List # using for loop # initializing list test_list = [""GeeksforGeeks"", ""Geeky"", ""Computers"", ""Algorithms""] # printing original list print(""The original list is: "" + str(test_list)) # initializing substring subs = ""Geek"" # using for loop count = 0 for string in test_list: if subs in string: count += 1 # printing result print(""All strings count with given substring are: "" + str(count)) #Output : The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] [END]" Python - Replace Substrings from String List,https://www.geeksforgeeks.org/python-replace-substrings-from-string-list/,"# Python3 code to demonstrate # Replace Substrings from String List # using loop + replace() + enumerate() # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using loop + replace() + enumerate() sub = dict(test_list2) for key, val in sub.items(): for idx, ele in enumerate(test_list1): if key in ele: test_list1[idx] = ele.replace(key, val) # printing result print(""The list after replacement : "" + str(test_list1))"," #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']","Python - Replace Substrings from String List # Python3 code to demonstrate # Replace Substrings from String List # using loop + replace() + enumerate() # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using loop + replace() + enumerate() sub = dict(test_list2) for key, val in sub.items(): for idx, ele in enumerate(test_list1): if key in ele: test_list1[idx] = ele.replace(key, val) # printing result print(""The list after replacement : "" + str(test_list1)) #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] [END]" Python - Replace Substrings from String List,https://www.geeksforgeeks.org/python-replace-substrings-from-string-list/,"# Python3 code to demonstrate # Replace Substrings from String List # using replace() + list comprehension # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using replace() + list comprehension res = [ sub.replace(sub2[0], sub2[1]) for sub in test_list1 for sub2 in test_list2 if sub2[0] in sub ] # printing result print(""The list after replacement : "" + str(res))"," #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']","Python - Replace Substrings from String List # Python3 code to demonstrate # Replace Substrings from String List # using replace() + list comprehension # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using replace() + list comprehension res = [ sub.replace(sub2[0], sub2[1]) for sub in test_list1 for sub2 in test_list2 if sub2[0] in sub ] # printing result print(""The list after replacement : "" + str(res)) #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] [END]" Python - Replace Substrings from String List,https://www.geeksforgeeks.org/python-replace-substrings-from-string-list/,"import re # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List using re module for sub in test_list2: test_list1 = [re.sub(sub[0], sub[1], ele) for ele in test_list1] # printing result print(""The list after replacement : "" + str(test_list1)) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']","Python - Replace Substrings from String List import re # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List using re module for sub in test_list2: test_list1 = [re.sub(sub[0], sub[1], ele) for ele in test_list1] # printing result print(""The list after replacement : "" + str(test_list1)) # This code is contributed by Edula Vinay Kumar Reddy #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] [END]" Python - Replace Substrings from String List,https://www.geeksforgeeks.org/python-replace-substrings-from-string-list/,"# Python3 code to demonstrate # Replace Substrings from String List # using list comprehension + reduce() + replace() method from functools import reduce # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using list comprehension + reduce() + replace() method sub = dict(test_list2) res = [reduce(lambda i, j: i.replace(*j), sub.items(), ele) for ele in test_list1] # printing result print(""The list after replacement : "" + str(res))"," #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']","Python - Replace Substrings from String List # Python3 code to demonstrate # Replace Substrings from String List # using list comprehension + reduce() + replace() method from functools import reduce # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using list comprehension + reduce() + replace() method sub = dict(test_list2) res = [reduce(lambda i, j: i.replace(*j), sub.items(), ele) for ele in test_list1] # printing result print(""The list after replacement : "" + str(res)) #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] [END]" Python - Replace Substrings from String List,https://www.geeksforgeeks.org/python-replace-substrings-from-string-list/,"# Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using a loop, dictionary, and string methods sub = {k: v for k, v in test_list2} res = [] for ele in test_list1: for key in sub: ele = ele.replace(key, sub[key]) res.append(ele) # printing result print(""The list after replacement : "" + str(res)) # This code is contributed by Vinay Pinjala."," #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']","Python - Replace Substrings from String List # Initializing list1 test_list1 = [""GeeksforGeeks"", ""is"", ""Best"", ""For"", ""Geeks"", ""And"", ""Computer Science""] test_list2 = [[""Geeks"", ""Gks""], [""And"", ""&""], [""Computer"", ""Comp""]] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Replace Substrings from String List # using a loop, dictionary, and string methods sub = {k: v for k, v in test_list2} res = [] for ele in test_list1: for key in sub: ele = ele.replace(key, sub[key]) res.append(ele) # printing result print(""The list after replacement : "" + str(res)) # This code is contributed by Vinay Pinjala. #Output : The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] [END]" Python - Group Sublists by another list,https://www.geeksforgeeks.org/python-group-sublists-by-another-list/,"# Python3 code to demonstrate # Group Sublists by another List # using loop + generator(yield) # helper function def grp_ele(test_list1, test_list2): temp = [] for i in test_list1: if i in test_list2: if temp: yield temp temp = [] yield i else: temp.append(i) if temp: yield temp # Initializing lists test_list1 = [8, 5, 9, 11, 3, 7] test_list2 = [9, 11] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Group Sublists by another List # using loop + generator(yield) res = list(grp_ele(test_list1, test_list2)) # printing result print(""The Grouped list is : "" + str(res))"," #Output : The original list 1 is : [8, 5, 9, 11, 3, 7]","Python - Group Sublists by another list # Python3 code to demonstrate # Group Sublists by another List # using loop + generator(yield) # helper function def grp_ele(test_list1, test_list2): temp = [] for i in test_list1: if i in test_list2: if temp: yield temp temp = [] yield i else: temp.append(i) if temp: yield temp # Initializing lists test_list1 = [8, 5, 9, 11, 3, 7] test_list2 = [9, 11] # printing original lists print(""The original list 1 is : "" + str(test_list1)) print(""The original list 2 is : "" + str(test_list2)) # Group Sublists by another List # using loop + generator(yield) res = list(grp_ele(test_list1, test_list2)) # printing result print(""The Grouped list is : "" + str(res)) #Output : The original list 1 is : [8, 5, 9, 11, 3, 7] [END]" Python | Remove Reduntant Substrings from String List,https://www.geeksforgeeks.org/python-remove-reduntant-substrings-from-strings-list/,"# Python3 code to demonstrate working of # Remove Redundant Substrings from Strings List # Using enumerate() + join() + sort() # initializing list test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] # printing original list print(""The original list : "" + str(test_list)) # using loop to iterate for each string test_list.sort(key=len) res = [] for idx, val in enumerate(test_list): # concatenating all next values and checking for existence if val not in "", "".join(test_list[idx + 1 :]): res.append(val) # printing result print(""The filtered list : "" + str(res))"," #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks']","Python | Remove Reduntant Substrings from String List # Python3 code to demonstrate working of # Remove Redundant Substrings from Strings List # Using enumerate() + join() + sort() # initializing list test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] # printing original list print(""The original list : "" + str(test_list)) # using loop to iterate for each string test_list.sort(key=len) res = [] for idx, val in enumerate(test_list): # concatenating all next values and checking for existence if val not in "", "".join(test_list[idx + 1 :]): res.append(val) # printing result print(""The filtered list : "" + str(res)) #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks'] [END]" Python | Remove Reduntant Substrings from String List,https://www.geeksforgeeks.org/python-remove-reduntant-substrings-from-strings-list/,"# Python3 code to demonstrate working of # Remove Redundant Substrings from Strings List # Using list comprehension + join() + enumerate() # initializing list test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] # printing original list print(""The original list : "" + str(test_list)) # using list comprehension to iterate for each string # and perform join in one liner test_list.sort(key=len) res = [ val for idx, val in enumerate(test_list) if val not in "", "".join(test_list[idx + 1 :]) ] # printing result print(""The filtered list : "" + str(res))"," #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks']","Python | Remove Reduntant Substrings from String List # Python3 code to demonstrate working of # Remove Redundant Substrings from Strings List # Using list comprehension + join() + enumerate() # initializing list test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] # printing original list print(""The original list : "" + str(test_list)) # using list comprehension to iterate for each string # and perform join in one liner test_list.sort(key=len) res = [ val for idx, val in enumerate(test_list) if val not in "", "".join(test_list[idx + 1 :]) ] # printing result print(""The filtered list : "" + str(res)) #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks'] [END]" Python | Remove Reduntant Substrings from String List,https://www.geeksforgeeks.org/python-remove-reduntant-substrings-from-strings-list/,"def remove_redundant_substrings(strings): # Base case: if the list is empty or has only one element, return it if len(strings) <= 1: return strings # Sort the list by length to simplify the recursion strings.sort(key=len) # Take the first string and remove it from the list current_string = strings.pop(0) # Recursively remove redundant substrings from the rest of the list remaining_strings = remove_redundant_substrings(strings) # Check if the current string is a redundant substring of any of the remaining strings for string in remaining_strings: if current_string in string: return remaining_strings # If the current string is not redundant, add it back to the list and return it remaining_strings.append(current_string) return remaining_strings test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] print(""The original list : "" + str(test_list)) res = remove_redundant_substrings(test_list) print(""The filtered list : "" + str(res))"," #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks']","Python | Remove Reduntant Substrings from String List def remove_redundant_substrings(strings): # Base case: if the list is empty or has only one element, return it if len(strings) <= 1: return strings # Sort the list by length to simplify the recursion strings.sort(key=len) # Take the first string and remove it from the list current_string = strings.pop(0) # Recursively remove redundant substrings from the rest of the list remaining_strings = remove_redundant_substrings(strings) # Check if the current string is a redundant substring of any of the remaining strings for string in remaining_strings: if current_string in string: return remaining_strings # If the current string is not redundant, add it back to the list and return it remaining_strings.append(current_string) return remaining_strings test_list = [""Gfg"", ""Gfg is best"", ""Geeks"", ""Gfg is for Geeks""] print(""The original list : "" + str(test_list)) res = remove_redundant_substrings(test_list) print(""The filtered list : "" + str(res)) #Output : The original list : ['Gfg', 'Gfg is best', 'Geeks', 'Gfg is for Geeks'] [END]" Python - Sort String by Custom Integer Substrings,https://www.geeksforgeeks.org/python-sort-string-by-custom-integer-substrings/,"# Python3 code to demonstrate working of # Sort String by Custom Substrings # Using sorted() + zip() + lambda + regex() import re # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating inverse mapping with index temp_dict = {val: key for key, val in enumerate(subord_list)} # custom sorting temp_list = sorted( [[ele, temp_dict[re.search(""(\d+)$"", ele).group()]] for ele in test_list], key=lambda x: x[1], ) # compiling result res = [ele for ele in list(zip(*temp_list))[0]] # printing result print(""The sorted list : "" + str(res))"," #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11']","Python - Sort String by Custom Integer Substrings # Python3 code to demonstrate working of # Sort String by Custom Substrings # Using sorted() + zip() + lambda + regex() import re # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating inverse mapping with index temp_dict = {val: key for key, val in enumerate(subord_list)} # custom sorting temp_list = sorted( [[ele, temp_dict[re.search(""(\d+)$"", ele).group()]] for ele in test_list], key=lambda x: x[1], ) # compiling result res = [ele for ele in list(zip(*temp_list))[0]] # printing result print(""The sorted list : "" + str(res)) #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11'] [END]" Python - Sort String by Custom Integer Substrings,https://www.geeksforgeeks.org/python-sort-string-by-custom-integer-substrings/,"# Python3 code to demonstrate working of # Sort String by Custom Substrings # Using sorted() + comparator + regex() import re # helper function to solve problem def hlper_fnc(ele): temp = re.search(""(\d+)$"", ele).group() return temp_dict[temp] if temp in temp_dict else int(temp) # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating inverse mapping with index temp_dict = {val: key for key, val in enumerate(test_list)} # sorting using comparator test_list.sort(key=lambda ele: hlper_fnc(ele)) # printing result print(""The sorted list : "" + str(test_list))"," #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11']","Python - Sort String by Custom Integer Substrings # Python3 code to demonstrate working of # Sort String by Custom Substrings # Using sorted() + comparator + regex() import re # helper function to solve problem def hlper_fnc(ele): temp = re.search(""(\d+)$"", ele).group() return temp_dict[temp] if temp in temp_dict else int(temp) # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating inverse mapping with index temp_dict = {val: key for key, val in enumerate(test_list)} # sorting using comparator test_list.sort(key=lambda ele: hlper_fnc(ele)) # printing result print(""The sorted list : "" + str(test_list)) #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11'] [END]" Python - Sort String by Custom Integer Substrings,https://www.geeksforgeeks.org/python-sort-string-by-custom-integer-substrings/,"# Python3 code to demonstrate working of # Sort String by Custom Substrings # Using dictionary and sorted() import re # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating dictionary to store mappings of substrings to orders order_dict = {} for i in range(len(subord_list)): order_dict[subord_list[i]] = i # sorting based on the values of the mappings using sorted() test_list = sorted(test_list, key=lambda x: order_dict[re.search(""(\d+)$"", x).group()]) # printing result print(""The sorted list : "" + str(test_list))"," #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11']","Python - Sort String by Custom Integer Substrings # Python3 code to demonstrate working of # Sort String by Custom Substrings # Using dictionary and sorted() import re # initializing list test_list = [""Good at 4"", ""Wake at 7"", ""Work till 6"", ""Sleep at 11""] # printing original list print(""The original list : "" + str(test_list)) # initializing substring list subord_list = [""6"", ""7"", ""4"", ""11""] # creating dictionary to store mappings of substrings to orders order_dict = {} for i in range(len(subord_list)): order_dict[subord_list[i]] = i # sorting based on the values of the mappings using sorted() test_list = sorted(test_list, key=lambda x: order_dict[re.search(""(\d+)$"", x).group()]) # printing result print(""The sorted list : "" + str(test_list)) #Output : The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11'] [END]" Python - Test if Substring occurs in specific position,https://www.geeksforgeeks.org/python-test-if-substring-occurs-in-specific-position/,"# Python3 code to demonstrate working of # Test if Substring occurs in specific position # Using loop # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using loop res = True k = 0 for idx in range(len(test_str)): if idx >= i and idx < j: if test_str[idx] != substr[k]: res = False break k = k + 1 # printing result print(""Does string contain substring at required position ? : "" + str(res))"," #Output : The original string is : Gfg is best","Python - Test if Substring occurs in specific position # Python3 code to demonstrate working of # Test if Substring occurs in specific position # Using loop # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using loop res = True k = 0 for idx in range(len(test_str)): if idx >= i and idx < j: if test_str[idx] != substr[k]: res = False break k = k + 1 # printing result print(""Does string contain substring at required position ? : "" + str(res)) #Output : The original string is : Gfg is best [END]" Python - Test if Substring occurs in specific position,https://www.geeksforgeeks.org/python-test-if-substring-occurs-in-specific-position/,"# Python3 code to demonstrate working of # Test if Substring occurs in specific position # Using string slicing # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using string slicing res = test_str[i:j] == substr # printing result print(""Does string contain substring at required position ? : "" + str(res))"," #Output : The original string is : Gfg is best","Python - Test if Substring occurs in specific position # Python3 code to demonstrate working of # Test if Substring occurs in specific position # Using string slicing # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using string slicing res = test_str[i:j] == substr # printing result print(""Does string contain substring at required position ? : "" + str(res)) #Output : The original string is : Gfg is best [END]" Python - Test if Substring occurs in specific position,https://www.geeksforgeeks.org/python-test-if-substring-occurs-in-specific-position/,"# Python3 code to demonstrate working of # Test if Substring occurs in specific position # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using string slicing res = False if test_str.find(substr) == i: res = True # printing result print(""Does string contain substring at required position ? : "" + str(res))"," #Output : The original string is : Gfg is best","Python - Test if Substring occurs in specific position # Python3 code to demonstrate working of # Test if Substring occurs in specific position # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using string slicing res = False if test_str.find(substr) == i: res = True # printing result print(""Does string contain substring at required position ? : "" + str(res)) #Output : The original string is : Gfg is best [END]" Python - Test if Substring occurs in specific position,https://www.geeksforgeeks.org/python-test-if-substring-occurs-in-specific-position/,"# Python3 code to demonstrate working of # Test if Substring occurs in specific position def substring_occurs_at_position(test_str, substr, i, j): if j > len(test_str): return False if test_str[i:j] == substr: return True return substring_occurs_at_position(test_str, substr, i + 1, j + 1) # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" result = substring_occurs_at_position(test_str, substr, i, j) # printing result print(""Does string contain substring at required position? "", result) # this code contributed by tvsk"," #Output : The original string is : Gfg is best","Python - Test if Substring occurs in specific position # Python3 code to demonstrate working of # Test if Substring occurs in specific position def substring_occurs_at_position(test_str, substr, i, j): if j > len(test_str): return False if test_str[i:j] == substr: return True return substring_occurs_at_position(test_str, substr, i + 1, j + 1) # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" result = substring_occurs_at_position(test_str, substr, i, j) # printing result print(""Does string contain substring at required position? "", result) # this code contributed by tvsk #Output : The original string is : Gfg is best [END]" Python - Test if Substring occurs in specific position,https://www.geeksforgeeks.org/python-test-if-substring-occurs-in-specific-position/,"import re # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using regular expression pattern = ""^"" + substr + ""$"" res = bool(re.search(pattern, test_str[i : j + 1])) # printing result print(""Does string contain substring at required position ? : "" + str(res))"," #Output : The original string is : Gfg is best","Python - Test if Substring occurs in specific position import re # initializing string test_str = ""Gfg is best"" # printing original string print(""The original string is : "" + test_str) # initializing range i, j = 7, 11 # initializing substr substr = ""best"" # Test if Substring occurs in specific position # Using regular expression pattern = ""^"" + substr + ""$"" res = bool(re.search(pattern, test_str[i : j + 1])) # printing result print(""Does string contain substring at required position ? : "" + str(res)) #Output : The original string is : Gfg is best [END]" Python program to check whether the string is Symmetrical or Palindrome,https://www.geeksforgeeks.org/python-program-to-check-whether-the-string-is-symmetrical-or-palindrome/?ref=leftbar-rightbar,"# Python program to demonstrate # symmetry and palindrome of the # string # Function to check whether the # string is palindrome or not def palindrome(a): # finding the mid, start # and last index of the string mid = (len(a) - 1) // 2 # you can remove the -1 or you add <= sign in line 21 start = 0 # so that you can compare the middle elements also. last = len(a) - 1 flag = 0 # A loop till the mid of the # string while start <= mid: # comparing letters from right # from the letters from left if a[start] == a[last]: start += 1 last -= 1 else: flag = 1 break # Checking the flag variable to # check if the string is palindrome # or not if flag == 0: print(""The entered string is palindrome"") else: print(""The entered string is not palindrome"") # Function to check whether the # string is symmetrical or not def symmetry(a): n = len(a) flag = 0 # Check if the string's length # is odd or even if n % 2: mid = n // 2 + 1 else: mid = n // 2 start1 = 0 start2 = mid while start1 < mid and start2 < n: if a[start1] == a[start2]: start1 = start1 + 1 start2 = start2 + 1 else: flag = 1 break # Checking the flag variable to # check if the string is symmetrical # or not if flag == 0: print(""The entered string is symmetrical"") else: print(""The entered string is not symmetrical"") # Driver code string = ""amaama"" palindrome(string) symmetry(string)","Input: khokho Output: ","Python program to check whether the string is Symmetrical or Palindrome # Python program to demonstrate # symmetry and palindrome of the # string # Function to check whether the # string is palindrome or not def palindrome(a): # finding the mid, start # and last index of the string mid = (len(a) - 1) // 2 # you can remove the -1 or you add <= sign in line 21 start = 0 # so that you can compare the middle elements also. last = len(a) - 1 flag = 0 # A loop till the mid of the # string while start <= mid: # comparing letters from right # from the letters from left if a[start] == a[last]: start += 1 last -= 1 else: flag = 1 break # Checking the flag variable to # check if the string is palindrome # or not if flag == 0: print(""The entered string is palindrome"") else: print(""The entered string is not palindrome"") # Function to check whether the # string is symmetrical or not def symmetry(a): n = len(a) flag = 0 # Check if the string's length # is odd or even if n % 2: mid = n // 2 + 1 else: mid = n // 2 start1 = 0 start2 = mid while start1 < mid and start2 < n: if a[start1] == a[start2]: start1 = start1 + 1 start2 = start2 + 1 else: flag = 1 break # Checking the flag variable to # check if the string is symmetrical # or not if flag == 0: print(""The entered string is symmetrical"") else: print(""The entered string is not symmetrical"") # Driver code string = ""amaama"" palindrome(string) symmetry(string) Input: khokho Output: [END]" Python program to check whether the string is Symmetrical or Palindrome,https://www.geeksforgeeks.org/python-program-to-check-whether-the-string-is-symmetrical-or-palindrome/?ref=leftbar-rightbar,"string = ""amaama"" half = int(len(string) / 2) first_str = string[:half] second_str = string[half:] # symmetric if first_str == second_str: print(string, ""string is symmetrical"") else: print(string, ""string is not symmetrical"") # palindrome if first_str == second_str[::-1]: # ''.join(reversed(second_str)) [slower] print(string, ""string is palindrome"") else: print(string, ""string is not palindrome"")","Input: khokho Output: ","Python program to check whether the string is Symmetrical or Palindrome string = ""amaama"" half = int(len(string) / 2) first_str = string[:half] second_str = string[half:] # symmetric if first_str == second_str: print(string, ""string is symmetrical"") else: print(string, ""string is not symmetrical"") # palindrome if first_str == second_str[::-1]: # ''.join(reversed(second_str)) [slower] print(string, ""string is palindrome"") else: print(string, ""string is not palindrome"") Input: khokho Output: [END]" Python program to check whether the string is Symmetrical or Palindrome,https://www.geeksforgeeks.org/python-program-to-check-whether-the-string-is-symmetrical-or-palindrome/?ref=leftbar-rightbar,"# Python program to check whether the string is Symmetrical or Palindrome import re input_str = ""amaama"" reversed_str = input_str[::-1] if input_str == reversed_str: print(""The entered string is symmetrical"") else: print(""The entered string is not symmetrical"") if re.match(""^(\w+)\Z"", input_str) and input_str == input_str[::-1]: print(""The entered string is palindrome"") else: print(""The entered string is not palindrome"")","Input: khokho Output: ","Python program to check whether the string is Symmetrical or Palindrome # Python program to check whether the string is Symmetrical or Palindrome import re input_str = ""amaama"" reversed_str = input_str[::-1] if input_str == reversed_str: print(""The entered string is symmetrical"") else: print(""The entered string is not symmetrical"") if re.match(""^(\w+)\Z"", input_str) and input_str == input_str[::-1]: print(""The entered string is palindrome"") else: print(""The entered string is not palindrome"") Input: khokho Output: [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"# Python code # To reverse words in a given string # input string string = ""geeks quiz practice code"" # reversing words in a given string s = string.split()[::-1] l = [] for i in s: # appending reversed words to l l.append(i) # printing reverse words print("" "".join(l))","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python # Python code # To reverse words in a given string # input string string = ""geeks quiz practice code"" # reversing words in a given string s = string.split()[::-1] l = [] for i in s: # appending reversed words to l l.append(i) # printing reverse words print("" "".join(l)) #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"# Function to reverse words of string def rev_sentence(sentence): # first split the string into words words = sentence.split("" "") # then reverse the split string list and join using space reverse_sentence = "" "".join(reversed(words)) # finally return the joined string return reverse_sentence if __name__ == ""__main__"": input = ""geeks quiz practice code"" print(rev_sentence(input))","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python # Function to reverse words of string def rev_sentence(sentence): # first split the string into words words = sentence.split("" "") # then reverse the split string list and join using space reverse_sentence = "" "".join(reversed(words)) # finally return the joined string return reverse_sentence if __name__ == ""__main__"": input = ""geeks quiz practice code"" print(rev_sentence(input)) #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"# Function to reverse words of string import re def rev_sentence(sentence): # find all the words in sentence words = re.findall(""\w+"", sentence) # Backward iterate over list of words and join using space reverse_sentence = "" "".join(words[i] for i in range(len(words) - 1, -1, -1)) # finally return the joined string return reverse_sentence if __name__ == ""__main__"": input = ""geeks quiz practice code"" print(rev_sentence(input))","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python # Function to reverse words of string import re def rev_sentence(sentence): # find all the words in sentence words = re.findall(""\w+"", sentence) # Backward iterate over list of words and join using space reverse_sentence = "" "".join(words[i] for i in range(len(words) - 1, -1, -1)) # finally return the joined string return reverse_sentence if __name__ == ""__main__"": input = ""geeks quiz practice code"" print(rev_sentence(input)) #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"# initializing string string = ""geeks quiz practice code"" # creating an empty stack stack = [] # pushing words onto the stack for word in string.split(): stack.append(word) # creating an empty list to store the reversed words reversed_words = [] # popping words off the stack and appending them to the list while stack: reversed_words.append(stack.pop()) # joining the reversed words with a space reversed_string = "" "".join(reversed_words) # printing the reversed string print(reversed_string) # This code is contributed by Edula Vinay Kumar Reddy","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python # initializing string string = ""geeks quiz practice code"" # creating an empty stack stack = [] # pushing words onto the stack for word in string.split(): stack.append(word) # creating an empty list to store the reversed words reversed_words = [] # popping words off the stack and appending them to the list while stack: reversed_words.append(stack.pop()) # joining the reversed words with a space reversed_string = "" "".join(reversed_words) # printing the reversed string print(reversed_string) # This code is contributed by Edula Vinay Kumar Reddy #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"def reverse_words(string): # split the string into a list of words words = string.split() # initialize an empty string to store the reversed words reversed_string = """" # loop through the words in reverse order and append them to the reversed string for i in range(len(words) - 1, -1, -1): reversed_string += words[i] + "" "" # remove the extra space at the end of the reversed string and return it return reversed_string.strip() # example usage string = ""geeks quiz practice code"" reversed_string = reverse_words(string) print(reversed_string) # output: ""code practice quiz geeks""","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python def reverse_words(string): # split the string into a list of words words = string.split() # initialize an empty string to store the reversed words reversed_string = """" # loop through the words in reverse order and append them to the reversed string for i in range(len(words) - 1, -1, -1): reversed_string += words[i] + "" "" # remove the extra space at the end of the reversed string and return it return reversed_string.strip() # example usage string = ""geeks quiz practice code"" reversed_string = reverse_words(string) print(reversed_string) # output: ""code practice quiz geeks"" #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Reverse words in a given String in Python,https://www.geeksforgeeks.org/reverse-words-given-string-python/,"# Python program for the above approach # Function to reverse the words in string def reverse_word(s, start, end): while start < end: s[start], s[end] = s[end], s[start] start += 1 end -= 1 # Function to reverse the string def reverse_string(s): s = list(s) start, end = 0, len(s) - 1 reverse_word(s, start, end) start = end = 0 # Iterate over the string S while end < len(s): if s[end] == "" "": reverse_word(s, start, end - 1) start = end + 1 end += 1 # Reverse the words reverse_word(s, start, end - 1) return """".join(s) # Driver Code S = ""geeks quiz practice code"" print(reverse_string(S))","#Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks???","Reverse words in a given String in Python # Python program for the above approach # Function to reverse the words in string def reverse_word(s, start, end): while start < end: s[start], s[end] = s[end], s[start] start += 1 end -= 1 # Function to reverse the string def reverse_string(s): s = list(s) start, end = 0, len(s) - 1 reverse_word(s, start, end) start = end = 0 # Iterate over the string S while end < len(s): if s[end] == "" "": reverse_word(s, start, end - 1) start = end + 1 end += 1 # Reverse the words reverse_word(s, start, end - 1) return """".join(s) # Driver Code S = ""geeks quiz practice code"" print(reverse_string(S)) #Input : str ="" geeks quiz practice code"" #Output : str = code practice quiz geeks??? [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"test_str = ""GeeksForGeeks"" # Removing char at pos 3 new_str = """" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print(""The string after removal of i'th character : "" + new_str)"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python test_str = ""GeeksForGeeks"" # Removing char at pos 3 new_str = """" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print(""The string after removal of i'th character : "" + new_str) #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"# Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using replace new_str = test_str.replace(""e"", """") # Printing string after removal # removes all occurrences of 'e' print(""The string after removal of i'th character( doesn't work) : "" + new_str) # Removing 1st occurrence of s, i.e 5th pos. # if we wish to remove it. new_str = test_str.replace(""s"", """", 1) # Printing string after removal # removes first occurrences of s print(""The string after removal of i'th character(works) : "" + new_str)"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python # Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using replace new_str = test_str.replace(""e"", """") # Printing string after removal # removes all occurrences of 'e' print(""The string after removal of i'th character( doesn't work) : "" + new_str) # Removing 1st occurrence of s, i.e 5th pos. # if we wish to remove it. new_str = test_str.replace(""s"", """", 1) # Printing string after removal # removes first occurrences of s print(""The string after removal of i'th character(works) : "" + new_str) #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"# Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using slice + concatenation new_str = test_str[:2] + test_str[3:] # Printing string after removal # removes ele. at 3rd index print(""The string after removal of i'th character : "" + new_str)"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python # Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using slice + concatenation new_str = test_str[:2] + test_str[3:] # Printing string after removal # removes ele. at 3rd index print(""The string after removal of i'th character : "" + new_str) #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"# Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using join() + list comprehension new_str = """".join([test_str[i] for i in range(len(test_str)) if i != 2]) # Printing string after removal # removes ele. at 3rd index print(""The string after removal of i'th character : "" + new_str)"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python # Initializing String test_str = ""GeeksForGeeks"" # Removing char at pos 3 # using join() + list comprehension new_str = """".join([test_str[i] for i in range(len(test_str)) if i != 2]) # Printing string after removal # removes ele. at 3rd index print(""The string after removal of i'th character : "" + new_str) #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"str = ""Geeks123For123Geeks"" print(str.translate({ord(i): None for i in ""123""}))"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python str = ""Geeks123For123Geeks"" print(str.translate({ord(i): None for i in ""123""})) #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"def remove_ith_character(s, i): # Base case: if index is 0, return string with first character removed if i == 0: return s[1:] # Recursive case: return first character concatenated with result of calling function on string with index decremented by 1 return s[0] + remove_ith_character(s[1:], i - 1) # Test the function test_str = ""GeeksForGeeks"" new_str = remove_ith_character(test_str, 2) print(""The string after removal of ith character:"", new_str) # This code is contributed by Edula Vinay Kumar Reddy"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python def remove_ith_character(s, i): # Base case: if index is 0, return string with first character removed if i == 0: return s[1:] # Recursive case: return first character concatenated with result of calling function on string with index decremented by 1 return s[0] + remove_ith_character(s[1:], i - 1) # Test the function test_str = ""GeeksForGeeks"" new_str = remove_ith_character(test_str, 2) print(""The string after removal of ith character:"", new_str) # This code is contributed by Edula Vinay Kumar Reddy #Output : The string after removal of i'th character : GeksForGeeks [END]" Ways to remove i - th character from string in python,https://www.geeksforgeeks.org/ways-to-remove-ith-character-from-string-in-python/,"def remove_char(s, i): b = bytearray(s, ""utf-8"") del b[i] return b.decode() # Example usage s = ""hello world"" i = 4 s = remove_char(s, i) print(s)"," #Output : The string after removal of i'th character : GeksForGeeks","Ways to remove i - th character from string in python def remove_char(s, i): b = bytearray(s, ""utf-8"") del b[i] return b.decode() # Example usage s = ""hello world"" i = 4 s = remove_char(s, i) print(s) #Output : The string after removal of i'th character : GeksForGeeks [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using len str = ""geeks"" print(len(str))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using len str = ""geeks"" print(len(str)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using for loop # Returns length of string def findLen(str): counter = 0 for i in str: counter += 1 return counter str = ""geeks"" print(findLen(str))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using for loop # Returns length of string def findLen(str): counter = 0 for i in str: counter += 1 return counter str = ""geeks"" print(findLen(str)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using while loop. # Returns length of string def findLen(str): counter = 0 while str[counter:]: counter += 1 return counter str = ""geeks"" print(findLen(str))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using while loop. # Returns length of string def findLen(str): counter = 0 while str[counter:]: counter += 1 return counter str = ""geeks"" print(findLen(str)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using join and count # Returns length of string def findLen(str): if not str: return 0 else: some_random_str = ""py"" return ((some_random_str).join(str)).count(some_random_str) + 1 str = ""geeks"" print(findLen(str))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using join and count # Returns length of string def findLen(str): if not str: return 0 else: some_random_str = ""py"" return ((some_random_str).join(str)).count(some_random_str) + 1 str = ""geeks"" print(findLen(str)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using reduce import functools def findLen(string): return functools.reduce(lambda x, y: x + 1, string, 0) # Driver Code string = ""geeks"" print(findLen(string))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using reduce import functools def findLen(string): return functools.reduce(lambda x, y: x + 1, string, 0) # Driver Code string = ""geeks"" print(findLen(string)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# Python code to demonstrate string length # using sum def findLen(string): return sum(1 for i in string) # Driver Code string = ""geeks"" print(findLen(string))","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # Python code to demonstrate string length # using sum def findLen(string): return sum(1 for i in string) # Driver Code string = ""geeks"" print(findLen(string)) #Input : 'abc' #Output : 3 [END]" Find length of a string in python (4 ways),https://www.geeksforgeeks.org/find-length-of-a-string-in-python-4-ways/,"# python code to find the length of # string using enumerate function string = ""gee@1ks"" s = 0 for i, a in enumerate(string): s += 1 print(s)","#Input : 'abc' #Output : 3","Find length of a string in python (4 ways) # python code to find the length of # string using enumerate function string = ""gee@1ks"" s = 0 for i, a in enumerate(string): s += 1 print(s) #Input : 'abc' #Output : 3 [END]" Python - Avoid Spaces in string ,https://www.geeksforgeeks.org/python-avoid-spaces-in-string-length/,"# Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # Using isspace() + sum() # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # isspace() checks for space # sum checks count res = sum(not chr.isspace() for chr in test_str) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res))"," #Output : The original string is : geeksforgeeks 33 is best","Python - Avoid Spaces in string # Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # Using isspace() + sum() # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # isspace() checks for space # sum checks count res = sum(not chr.isspace() for chr in test_str) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res)) #Output : The original string is : geeksforgeeks 33 is best [END]" Python - Avoid Spaces in string ,https://www.geeksforgeeks.org/python-avoid-spaces-in-string-length/,"# Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # Using sum() + len() + map() + split() # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # len() finds individual word Frequency # sum() extracts final Frequency res = sum(map(len, test_str.split())) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res))"," #Output : The original string is : geeksforgeeks 33 is best","Python - Avoid Spaces in string # Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # Using sum() + len() + map() + split() # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # len() finds individual word Frequency # sum() extracts final Frequency res = sum(map(len, test_str.split())) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res)) #Output : The original string is : geeksforgeeks 33 is best [END]" Python - Avoid Spaces in string ,https://www.geeksforgeeks.org/python-avoid-spaces-in-string-length/,"# Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) test_str = test_str.replace("" "", """") res = len(test_str) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res))"," #Output : The original string is : geeksforgeeks 33 is best","Python - Avoid Spaces in string # Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) test_str = test_str.replace("" "", """") res = len(test_str) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res)) #Output : The original string is : geeksforgeeks 33 is best [END]" Python - Avoid Spaces in string ,https://www.geeksforgeeks.org/python-avoid-spaces-in-string-length/,"# Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "", test_str) # initializing count = 0 count = 0 # loop to iterate the string, character by character for i in test_str: if i == "" "": continue count += 1 # printing result print(""The Characters Frequency avoiding spaces : "", count) # This code is contributed by Pratik Gupta (guptapratik)"," #Output : The original string is : geeksforgeeks 33 is best","Python - Avoid Spaces in string # Python3 code to demonstrate working of # Avoid Spaces in Characters Frequency # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "", test_str) # initializing count = 0 count = 0 # loop to iterate the string, character by character for i in test_str: if i == "" "": continue count += 1 # printing result print(""The Characters Frequency avoiding spaces : "", count) # This code is contributed by Pratik Gupta (guptapratik) #Output : The original string is : geeksforgeeks 33 is best [END]" Python - Avoid Spaces in string ,https://www.geeksforgeeks.org/python-avoid-spaces-in-string-length/,"# initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # Using a list comprehension and the join() function res = len("""".join([char for char in test_str if char != "" ""])) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res))"," #Output : The original string is : geeksforgeeks 33 is best","Python - Avoid Spaces in string # initializing string test_str = ""geeksforgeeks 33 is best"" # printing original string print(""The original string is : "" + str(test_str)) # Using a list comprehension and the join() function res = len("""".join([char for char in test_str if char != "" ""])) # printing result print(""The Characters Frequency avoiding spaces : "" + str(res)) #Output : The original string is : geeksforgeeks 33 is best [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# Python code # To print even length words in string # input string n = ""This is a python language"" # splitting the words in a given string s = n.split("" "") for i in s: # checking the length of words if len(i) % 2 == 0: print(i) # this code is contributed by gangarajula laxmi","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # Python code # To print even length words in string # input string n = ""This is a python language"" # splitting the words in a given string s = n.split("" "") for i in s: # checking the length of words if len(i) % 2 == 0: print(i) # this code is contributed by gangarajula laxmi Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# Python3 program to print # even length words in a string def printWords(s): # split the string s = s.split("" "") # iterate in words of string for word in s: # if length is even if len(word) % 2 == 0: print(word) # Driver Code s = ""i am muskan"" printWords(s)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # Python3 program to print # even length words in a string def printWords(s): # split the string s = s.split("" "") # iterate in words of string for word in s: # if length is even if len(word) % 2 == 0: print(word) # Driver Code s = ""i am muskan"" printWords(s) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# Python code # To print even length words in string # input string n = ""geeks for geek"" # splitting the words in a given string s = n.split("" "") l = list(filter(lambda x: (len(x) % 2 == 0), s)) print(l)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # Python code # To print even length words in string # input string n = ""geeks for geek"" # splitting the words in a given string s = n.split("" "") l = list(filter(lambda x: (len(x) % 2 == 0), s)) print(l) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# python code to print even length words n = ""geeks for geek"" s = n.split("" "") print([x for x in s if len(x) % 2 == 0])","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # python code to print even length words n = ""geeks for geek"" s = n.split("" "") print([x for x in s if len(x) % 2 == 0]) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# python code to print even length words n = ""geeks for geek"" s = n.split("" "") print([x for i, x in enumerate(s) if len(x) % 2 == 0])","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # python code to print even length words n = ""geeks for geek"" s = n.split("" "") print([x for i, x in enumerate(s) if len(x) % 2 == 0]) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# recursive function to print even length words def PrintEvenLengthWord(itr, list1): if itr == len(list1): return if len(list1[itr]) % 2 == 0: print(list1[itr]) PrintEvenLengthWord(itr + 1, list1) return str = ""geeks for geek"" l = [i for i in str.split()] PrintEvenLengthWord(0, l)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # recursive function to print even length words def PrintEvenLengthWord(itr, list1): if itr == len(list1): return if len(list1[itr]) % 2 == 0: print(list1[itr]) PrintEvenLengthWord(itr + 1, list1) return str = ""geeks for geek"" l = [i for i in str.split()] PrintEvenLengthWord(0, l) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"s = ""geeks for geek"" # Split the string into a list of words words = s.split("" "") # Use the filter function and lambda function to find even length words even_length_words = list(filter(lambda x: len(x) % 2 == 0, words)) # Print the list of even length words print(even_length_words) # This code is contributed by Edula Vinay Kumar Reddy","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string s = ""geeks for geek"" # Split the string into a list of words words = s.split("" "") # Use the filter function and lambda function to find even length words even_length_words = list(filter(lambda x: len(x) % 2 == 0, words)) # Print the list of even length words print(even_length_words) # This code is contributed by Edula Vinay Kumar Reddy Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"import itertools s = ""geeks for geek"" # Split the string into a list of words words = s.split("" "") # Use the itertools.filterfalse function and lambda function to find even length words even_length_words = list(itertools.filterfalse(lambda x: len(x) % 2 != 0, words)) # Print the list of even length words print(even_length_words)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string import itertools s = ""geeks for geek"" # Split the string into a list of words words = s.split("" "") # Use the itertools.filterfalse function and lambda function to find even length words even_length_words = list(itertools.filterfalse(lambda x: len(x) % 2 != 0, words)) # Print the list of even length words print(even_length_words) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# code def print_even_length_words(s): words = s.replace("","", "" "").replace(""."", "" "").split() for word in words: if len(word) % 2 == 0: print(word, end="" "") s = ""geeks for geek"" print_even_length_words(s)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # code def print_even_length_words(s): words = s.replace("","", "" "").replace(""."", "" "").split() for word in words: if len(word) % 2 == 0: print(word, end="" "") s = ""geeks for geek"" print_even_length_words(s) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"# Initialize the input string s = ""This is a python language"" # Initialize an empty string variable to store the current word being parsed word = """" # Iterate over each character in the input string for i in s: # If the character is a space, it means the current word has ended if i == "" "": # Check if the length of the current word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the current word print(word, end="" "") # Reset the current word variable for the next word word = """" # If the character is not a space, it means the current word is being formed else: # Append the character to the current word variable word += i # After the loop has ended, check if the length of the last word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the last word print(word, end="" "")","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string # Initialize the input string s = ""This is a python language"" # Initialize an empty string variable to store the current word being parsed word = """" # Iterate over each character in the input string for i in s: # If the character is a space, it means the current word has ended if i == "" "": # Check if the length of the current word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the current word print(word, end="" "") # Reset the current word variable for the next word word = """" # If the character is not a space, it means the current word is being formed else: # Append the character to the current word variable word += i # After the loop has ended, check if the length of the last word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the last word print(word, end="" "") Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"from itertools import compress n = ""This is a python language"" s = n.split("" "") even_len_bool = [len(word) % 2 == 0 for word in s] even_len_words = list(compress(s, even_len_bool)) print(even_len_words)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string from itertools import compress n = ""This is a python language"" s = n.split("" "") even_len_bool = [len(word) % 2 == 0 for word in s] even_len_words = list(compress(s, even_len_bool)) print(even_len_words) Input: s = ""This is a python language"" Output: This is python language [END]" Python program to print even length words in a string,https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/,"s = ""I am omkhar"" word_start = 0 even_words = [] for i in range(len(s)): if s[i] == "" "": word_end = i word_length = word_end - word_start if word_length % 2 == 0: even_words.append(s[word_start:word_end]) word_start = i + 1 word_length = len(s) - word_start if word_length % 2 == 0: even_words.append(s[word_start:]) for w in even_words: print(w)","Input: s = ""This is a python language"" Output: This is python language","Python program to print even length words in a string s = ""I am omkhar"" word_start = 0 even_words = [] for i in range(len(s)): if s[i] == "" "": word_end = i word_length = word_end - word_start if word_length % 2 == 0: even_words.append(s[word_start:word_end]) word_start = i + 1 word_length = len(s) - word_start if word_length % 2 == 0: even_words.append(s[word_start:]) for w in even_words: print(w) Input: s = ""This is a python language"" Output: This is python language [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"# Python3 code to demonstrate working of # Uppercase Half String # Using upper() + loop + len() # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 res = """" for idx in range(len(test_str)): # uppercasing later half if idx >= hlf_idx: res += test_str[idx].upper() else: res += test_str[idx] # printing result print(""The resultant string : "" + str(res))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using upper() + loop + len() # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 res = """" for idx in range(len(test_str)): # uppercasing later half if idx >= hlf_idx: res += test_str[idx].upper() else: res += test_str[idx] # printing result print(""The resultant string : "" + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"# Python3 code to demonstrate working of # Uppercase Half String # Using list comprehension + join() + upper() # Initializing string test_str = ""geeksforgeeks"" # Printing original string print(""The original string is : "" + str(test_str)) # Computing half index hlf_idx = len(test_str) // 2 # join() used to create result string res = """".join( [ test_str[idx].upper() if idx >= hlf_idx else test_str[idx] for idx in range(len(test_str)) ] ) # Printing result print(""The resultant string : "" + str(res))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using list comprehension + join() + upper() # Initializing string test_str = ""geeksforgeeks"" # Printing original string print(""The original string is : "" + str(test_str)) # Computing half index hlf_idx = len(test_str) // 2 # join() used to create result string res = """".join( [ test_str[idx].upper() if idx >= hlf_idx else test_str[idx] for idx in range(len(test_str)) ] ) # Printing result print(""The resultant string : "" + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"# Python3 code to demonstrate working of # Uppercase Half String # Using upper() + slicing string # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case # Using slicing # slicing takes one position less to ending position provided res = test_str[:hlf_idx] + test_str[hlf_idx:].upper() # printing result print(""The resultant string : "" + str(res))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using upper() + slicing string # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case # Using slicing # slicing takes one position less to ending position provided res = test_str[:hlf_idx] + test_str[hlf_idx:].upper() # printing result print(""The resultant string : "" + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"# Python3 code to demonstrate working of # Uppercase Half String # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index x = len(test_str) // 2 a = test_str[x:] b = test_str[:x] cap = ""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" sma = ""abcdefghijklmnopqrstuvwxyz"" res = """" for i in a: res += cap[sma.index(i)] res = b + res # printing result print(""The resultant string : "" + str(res))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index x = len(test_str) // 2 a = test_str[x:] b = test_str[:x] cap = ""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" sma = ""abcdefghijklmnopqrstuvwxyz"" res = """" for i in a: res += cap[sma.index(i)] res = b + res # printing result print(""The resultant string : "" + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"import re # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case using regular expressions res = re.sub(r""(?i)(\w+)"", lambda m: m.group().upper(), test_str[hlf_idx:]) # concatenating the two parts of the string result = test_str[:hlf_idx] + res # printing result print(""The resultant string : "" + str(result))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String import re # initializing string test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case using regular expressions res = re.sub(r""(?i)(\w+)"", lambda m: m.group().upper(), test_str[hlf_idx:]) # concatenating the two parts of the string result = test_str[:hlf_idx] + res # printing result print(""The resultant string : "" + str(result)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"# Python3 code to demonstrate working of # Uppercase Half String # Using string concatenation and conditional operator # Iializing string test_str = ""geeksforgeeks"" # Printing original string print(""The original string is : "" + str(test_str)) # Computing half index length = len(test_str) hlf_idx = length // 2 # Initializing result string result = """" # Iterating through characters for i in range(length): # Conditional operator result += test_str[i] if i < hlf_idx else test_str[i].upper() # Printing result print(""The resultant string : "" + str(result))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using string concatenation and conditional operator # Iializing string test_str = ""geeksforgeeks"" # Printing original string print(""The original string is : "" + str(test_str)) # Computing half index length = len(test_str) hlf_idx = length // 2 # Initializing result string result = """" # Iterating through characters for i in range(length): # Conditional operator result += test_str[i] if i < hlf_idx else test_str[i].upper() # Printing result print(""The resultant string : "" + str(result)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"from itertools import islice test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) hlf_idx = len(test_str) // 2 res = """".join( [c.upper() if i >= hlf_idx else c for i, c in enumerate(islice(test_str, None))] ) # printing result print(""The resultant string : "" + str(res)) # This code is contributed by Jyothi pinjala.","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String from itertools import islice test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) hlf_idx = len(test_str) // 2 res = """".join( [c.upper() if i >= hlf_idx else c for i, c in enumerate(islice(test_str, None))] ) # printing result print(""The resultant string : "" + str(res)) # This code is contributed by Jyothi pinjala. #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python - Uppercase Half String,https://www.geeksforgeeks.org/python-uppercase-half-string/,"test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) hlf_idx = len(test_str) // 2 res = """".join(map(lambda c: c.upper() if test_str.index(c) >= hlf_idx else c, test_str)) # printing result print(""The resultant string : "" + str(res))","#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE","Python - Uppercase Half String test_str = ""geeksforgeeks"" # printing original string print(""The original string is : "" + str(test_str)) hlf_idx = len(test_str) // 2 res = """".join(map(lambda c: c.upper() if test_str.index(c) >= hlf_idx else c, test_str)) # printing result print(""The resultant string : "" + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]" Python program to capitalize the first and last character of each word in a string,https://www.geeksforgeeks.org/python-program-to-capitalize-the-first-and-last-character-of-each-word-in-a-string/,"# Python program to capitalize # first and last character of # each word of a String # Function to do the same def word_both_cap(str): # lambda function for capitalizing the # first and last letter of words in # the string return "" "".join(map(lambda s: s[:-1] + s[-1].upper(), s.title().split())) # Driver's code s = ""welcome to geeksforgeeks"" print(""String before:"", s) print(""String after:"", word_both_cap(str))","Input: hello world Output: HellO WorlD","Python program to capitalize the first and last character of each word in a string # Python program to capitalize # first and last character of # each word of a String # Function to do the same def word_both_cap(str): # lambda function for capitalizing the # first and last letter of words in # the string return "" "".join(map(lambda s: s[:-1] + s[-1].upper(), s.title().split())) # Driver's code s = ""welcome to geeksforgeeks"" print(""String before:"", s) print(""String after:"", word_both_cap(str)) Input: hello world Output: HellO WorlD [END]" Python program to capitalize the first and last character of each word in a string,https://www.geeksforgeeks.org/python-program-to-capitalize-the-first-and-last-character-of-each-word-in-a-string/,"# Python program to capitalize # first and last character of # each word of a String s = ""welcome to geeksforgeeks"" print(""String before:"", s) a = s.split() res = [] for i in a: x = i[0].upper() + i[1:-1] + i[-1].upper() res.append(x) res = "" "".join(res) print(""String after:"", res)","Input: hello world Output: HellO WorlD","Python program to capitalize the first and last character of each word in a string # Python program to capitalize # first and last character of # each word of a String s = ""welcome to geeksforgeeks"" print(""String before:"", s) a = s.split() res = [] for i in a: x = i[0].upper() + i[1:-1] + i[-1].upper() res.append(x) res = "" "".join(res) print(""String after:"", res) Input: hello world Output: HellO WorlD [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i.isalpha(): flag_l = True # if string has number if i.isdigit(): flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks""))","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i.isalpha(): flag_l = True # if string has number if i.isdigit(): flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) Input: welcome2ourcountry34 Output: True [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i in ""abcdefghijklmnopqrstuvwxyz"": flag_l = True # if string has number if i in ""0123456789"": flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks""))","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i in ""abcdefghijklmnopqrstuvwxyz"": flag_l = True # if string has number if i in ""0123456789"": flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) Input: welcome2ourcountry34 Output: True [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"import operator as op def checkString(str): letters = ""abcdefghijklmnopqrstuvwxyz"" digits = ""0123456789"" # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if op.countOf(letters, i) > 0: flag_l = True # if string has digits if op.countOf(digits, i) > 0: flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks""))","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number import operator as op def checkString(str): letters = ""abcdefghijklmnopqrstuvwxyz"" digits = ""0123456789"" # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if op.countOf(letters, i) > 0: flag_l = True # if string has digits if op.countOf(digits, i) > 0: flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) Input: welcome2ourcountry34 Output: True [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"import re def checkString(str): # using regular expression to check if a string contains # at least one letter and one number match = re.search(r""[a-zA-Z]+"", str) and re.search(r""[0-9]+"", str) if match: return True else: return False # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) # This code is contributed by Vinay Pinjala.","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number import re def checkString(str): # using regular expression to check if a string contains # at least one letter and one number match = re.search(r""[a-zA-Z]+"", str) and re.search(r""[0-9]+"", str) if match: return True else: return False # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) # This code is contributed by Vinay Pinjala. Input: welcome2ourcountry34 Output: True [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"def checkString(str): # Create sets of letters and digits letters = set(""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"") digits = set(""0123456789"") # Check if the intersection of the input string and the sets of letters and digits is not empty return bool(letters & set(str)) and bool(digits & set(str)) # Test the function with two sample inputs print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) # This code is contributed by Jyothi pinjala.","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number def checkString(str): # Create sets of letters and digits letters = set(""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"") digits = set(""0123456789"") # Check if the intersection of the input string and the sets of letters and digits is not empty return bool(letters & set(str)) and bool(digits & set(str)) # Test the function with two sample inputs print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) # This code is contributed by Jyothi pinjala. Input: welcome2ourcountry34 Output: True [END]" Python program to check if a string has at least one letter and one number,https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/,"checkString = lambda s: any(c.isalpha() for c in s) and any(c.isdigit() for c in s) # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks""))","Input: welcome2ourcountry34 Output: True","Python program to check if a string has at least one letter and one number checkString = lambda s: any(c.isalpha() for c in s) and any(c.isdigit() for c in s) # driver code print(checkString(""thishasboth29"")) print(checkString(""geeksforgeeks"")) Input: welcome2ourcountry34 Output: True [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): string = string.lower() # set() function convert ""aeiou"" # string into set of characters # i.e.vowels = {'a', 'e', 'i', 'o', 'u'} vowels = set(""aeiou"") # set() function convert empty # dictionary into empty set s = set({}) # looping through each # character of the string for char in string: # Check for the character is present inside # the vowels set or not. If present, then # add into the set s by using add method if char in vowels: s.add(char) else: pass # check the length of set s equal to length # of vowels set or not. If equal, string is # accepted otherwise not if len(s) == len(vowels): print(""Accepted"") else: print(""Not Accepted"") # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" # calling function check(string)","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): string = string.lower() # set() function convert ""aeiou"" # string into set of characters # i.e.vowels = {'a', 'e', 'i', 'o', 'u'} vowels = set(""aeiou"") # set() function convert empty # dictionary into empty set s = set({}) # looping through each # character of the string for char in string: # Check for the character is present inside # the vowels set or not. If present, then # add into the set s by using add method if char in vowels: s.add(char) else: pass # check the length of set s equal to length # of vowels set or not. If equal, string is # accepted otherwise not if len(s) == len(vowels): print(""Accepted"") else: print(""Not Accepted"") # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" # calling function check(string) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"def check(string): string = string.replace("" "", """") string = string.lower() vowel = [ string.count(""a""), string.count(""e""), string.count(""i""), string.count(""o""), string.count(""u""), ] # If 0 is present int vowel count array if vowel.count(0) > 0: return ""not accepted"" else: return ""accepted"" # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" print(check(string))","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels def check(string): string = string.replace("" "", """") string = string.lower() vowel = [ string.count(""a""), string.count(""e""), string.count(""i""), string.count(""o""), string.count(""u""), ] # If 0 is present int vowel count array if vowel.count(0) > 0: return ""not accepted"" else: return ""accepted"" # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# Python program for the above approach def check(string): if len(set(string.lower()).intersection(""aeiou"")) >= 5: return ""accepted"" else: return ""not accepted"" # Driver code if __name__ == ""__main__"": string = ""geeksforgeeks"" print(check(string))","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # Python program for the above approach def check(string): if len(set(string.lower()).intersection(""aeiou"")) >= 5: return ""accepted"" else: return ""not accepted"" # Driver code if __name__ == ""__main__"": string = ""geeksforgeeks"" print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# import library import re sampleInput = ""aeioAEiuioea"" # regular expression to find the strings # which have characters other than a,e,i,o and u c = re.compile(""[^aeiouAEIOU]"") # use findall() to get the list of strings # that have characters other than a,e,i,o and u. if len(c.findall(sampleInput)): print(""Not Accepted"") # if length of list > 0 then it is not accepted else: print(""Accepted"") # if length of list = 0 then it is accepted","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # import library import re sampleInput = ""aeioAEiuioea"" # regular expression to find the strings # which have characters other than a,e,i,o and u c = re.compile(""[^aeiouAEIOU]"") # use findall() to get the list of strings # that have characters other than a,e,i,o and u. if len(c.findall(sampleInput)): print(""Not Accepted"") # if length of list > 0 then it is not accepted else: print(""Accepted"") # if length of list = 0 then it is accepted #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# Python | Program to accept the strings which contains all vowels def all_vowels(str_value): new_list = [char for char in str_value.lower() if char in ""aeiou""] if new_list: dic, lst = {}, [] for char in new_list: dic[""a""] = new_list.count(""a"") dic[""e""] = new_list.count(""e"") dic[""i""] = new_list.count(""i"") dic[""o""] = new_list.count(""o"") dic[""u""] = new_list.count(""u"") for i, j in dic.items(): if j == 0: lst.append(i) if lst: return f""All vowels except {','.join(lst)} are not present"" else: return ""All vowels are present"" else: return ""No vowels present"" # function-call str_value = ""geeksforgeeks"" print(all_vowels(str_value)) str_value = ""ABeeIghiObhkUul"" print(all_vowels(str_value)) # contribute by saikot","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # Python | Program to accept the strings which contains all vowels def all_vowels(str_value): new_list = [char for char in str_value.lower() if char in ""aeiou""] if new_list: dic, lst = {}, [] for char in new_list: dic[""a""] = new_list.count(""a"") dic[""e""] = new_list.count(""e"") dic[""i""] = new_list.count(""i"") dic[""o""] = new_list.count(""o"") dic[""u""] = new_list.count(""u"") for i, j in dic.items(): if j == 0: lst.append(i) if lst: return f""All vowels except {','.join(lst)} are not present"" else: return ""All vowels are present"" else: return ""No vowels present"" # function-call str_value = ""geeksforgeeks"" print(all_vowels(str_value)) str_value = ""ABeeIghiObhkUul"" print(all_vowels(str_value)) # contribute by saikot #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): # Checking if ""aeiou"" is a subset of the set of all letters in the string if set(""aeiou"").issubset(set(string.lower())): return ""Accepted"" return ""Not accepted"" # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" # calling function print(check(string))","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): # Checking if ""aeiou"" is a subset of the set of all letters in the string if set(""aeiou"").issubset(set(string.lower())): return ""Accepted"" return ""Not accepted"" # Driver code if __name__ == ""__main__"": string = ""SEEquoiaL"" # calling function print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"import collections def check(string): # create a Counter object to count the occurrences of each character counter = collections.Counter(string.lower()) # set of vowels vowels = set(""aeiou"") # counter for the number of vowels present vowel_count = 0 # check if each vowel is present in the string for vowel in vowels: if counter[vowel] > 0: vowel_count += 1 # check if all vowels are present if vowel_count == len(vowels): print(""Accepted"") else: print(""Not Accepted"") # test the function string = ""SEEquoiaL"" check(string)","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels import collections def check(string): # create a Counter object to count the occurrences of each character counter = collections.Counter(string.lower()) # set of vowels vowels = set(""aeiou"") # counter for the number of vowels present vowel_count = 0 # check if each vowel is present in the string for vowel in vowels: if counter[vowel] > 0: vowel_count += 1 # check if all vowels are present if vowel_count == len(vowels): print(""Accepted"") else: print(""Not Accepted"") # test the function string = ""SEEquoiaL"" check(string) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not # using all() method def check(string): vowels = ""aeiou"" # storing vowels if all(vowel in string.lower() for vowel in vowels): return ""Accepted"" return ""Not accepted"" # initializing string string = ""SEEquoiaL"" # test the function print(check(string)) # this code contributed by tvsk","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not # using all() method def check(string): vowels = ""aeiou"" # storing vowels if all(vowel in string.lower() for vowel in vowels): return ""Accepted"" return ""Not accepted"" # initializing string string = ""SEEquoiaL"" # test the function print(check(string)) # this code contributed by tvsk #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Program to accept the strings which contains all vowelsvowels,https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/,"# function definition def check(s): A = {""a"", ""e"", ""i"", ""o"", ""u""} # using the set difference if len(A.difference(set(s.lower()))) == 0: print(""accepted"") else: print(""not accepted"") # input s = ""SEEquoiaL"" # function call check(s)","#Input : geeksforgeeks #Output : Not Accepted","Python | Program to accept the strings which contains all vowelsvowels # function definition def check(s): A = {""a"", ""e"", ""i"", ""o"", ""u""} # using the set difference if len(A.difference(set(s.lower()))) == 0: print(""accepted"") else: print(""not accepted"") # input s = ""SEEquoiaL"" # function call check(s) #Input : geeksforgeeks #Output : Not Accepted [END]" Python | Count the Number of matching characters in a pair of string,https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/,"// C++ code to count number of matching// characters in a pair of strings??????# include using namespace std??????// Function to count the matching charactersvoid count(string str1, string str2){????????????????????????int c = 0, j = 0??????????????????????????????// Traverse the string 1 char by char????????????????????????for (int i=0??????????????????????????????????????????????????????i < str1.length()??????????????????????????????????????????????????????i++) {??????????????????????????????????????????????????????// This will check if str1[i]????????????????????????????????????????????????// is present in str2 or not????????????????????????????????????????????????// str2.find(str1[i]) returns - 1 if not found?????????????????????????????????????????????""No. of matching characters are: ""????????<< c / 2}??// Driver codeint main(){????????string str1 = ""aabcddekll12@""????????string str2 = ""bb2211@55k""??????????count(str1, str2)}","#Input : str1 = 'abcdef' str2 = 'defghia'","Python | Count the Number of matching characters in a pair of string // C++ code to count number of matching// characters in a pair of strings??????# include using namespace std??????// Function to count the matching charactersvoid count(string str1, string str2){????????????????????????int c = 0, j = 0??????????????????????????????// Traverse the string 1 char by char????????????????????????for (int i=0??????????????????????????????????????????????????????i < str1.length()??????????????????????????????????????????????????????i++) {??????????????????????????????????????????????????????// This will check if str1[i]????????????????????????????????????????????????// is present in str2 or not????????????????????????????????????????????????// str2.find(str1[i]) returns - 1 if not found?????????????????????????????????????????????""No. of matching characters are: ""????????<< c / 2}??// Driver codeint main(){????????string str1 = ""aabcddekll12@""????????string str2 = ""bb2211@55k""??????????count(str1, str2)} #Input : str1 = 'abcdef' str2 = 'defghia' [END]" Python | Count the Number of matching characters in a pair of string,https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/,"# Python code to count number of unique matching # characters in a pair of strings # count function count the common unique # characters present in both strings . def count(str1, str2): # set of characters of string1 set_string1 = set(str1) # set of characters of string2 set_string2 = set(str2) # using (&) intersection mathematical operation on sets # the unique characters present in both the strings # are stored in matched_characters set variable matched_characters = set_string1 & set_string2 # printing the length of matched_characters set # gives the no. of matched characters print(""No. of matching characters are : "" + str(len(matched_characters))) # Driver code if __name__ == ""__main__"": str1 = ""aabcddekll12@"" # first string str2 = ""bb2211@55k"" # second string # call count function count(str1, str2)","#Input : str1 = 'abcdef' str2 = 'defghia'","Python | Count the Number of matching characters in a pair of string # Python code to count number of unique matching # characters in a pair of strings # count function count the common unique # characters present in both strings . def count(str1, str2): # set of characters of string1 set_string1 = set(str1) # set of characters of string2 set_string2 = set(str2) # using (&) intersection mathematical operation on sets # the unique characters present in both the strings # are stored in matched_characters set variable matched_characters = set_string1 & set_string2 # printing the length of matched_characters set # gives the no. of matched characters print(""No. of matching characters are : "" + str(len(matched_characters))) # Driver code if __name__ == ""__main__"": str1 = ""aabcddekll12@"" # first string str2 = ""bb2211@55k"" # second string # call count function count(str1, str2) #Input : str1 = 'abcdef' str2 = 'defghia' [END]" Python | Count the Number of matching characters in a pair of string,https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/,"def count(str1, str2): # Initialize an empty dictionary to keep track of the count of each character in str1. char_count = {} # Iterate over each character in str1. for char in str1: # If the character is already in the dictionary, increment its count. if char in char_count: char_count[char] += 1 # Otherwise, add the character to the dictionary with a count of 1. else: char_count[char] = 1 # Initialize a counter variable to 0. counter = 0 # Iterate over each character in str2. for char in str2: # If the character is in the char_count dictionary and its count is greater than 0, increment the counter and decrement the count. if char in char_count and char_count[char] > 0: counter += 1 char_count[char] -= 1 # Print the number of matching characters. print(""No. of matching characters are: "" + str(counter)) # Driver code if __name__ == ""__main__"": # Define two strings to compare. str1 = ""aabcddekll12@"" str2 = ""bb2211@55k"" # Call the count function with the two strings. count(str1, str2)","#Input : str1 = 'abcdef' str2 = 'defghia'","Python | Count the Number of matching characters in a pair of string def count(str1, str2): # Initialize an empty dictionary to keep track of the count of each character in str1. char_count = {} # Iterate over each character in str1. for char in str1: # If the character is already in the dictionary, increment its count. if char in char_count: char_count[char] += 1 # Otherwise, add the character to the dictionary with a count of 1. else: char_count[char] = 1 # Initialize a counter variable to 0. counter = 0 # Iterate over each character in str2. for char in str2: # If the character is in the char_count dictionary and its count is greater than 0, increment the counter and decrement the count. if char in char_count and char_count[char] > 0: counter += 1 char_count[char] -= 1 # Print the number of matching characters. print(""No. of matching characters are: "" + str(counter)) # Driver code if __name__ == ""__main__"": # Define two strings to compare. str1 = ""aabcddekll12@"" str2 = ""bb2211@55k"" # Call the count function with the two strings. count(str1, str2) #Input : str1 = 'abcdef' str2 = 'defghia' [END]" Python program to count number of vowels using sets in given string,https://www.geeksforgeeks.org/python-program-count-number-vowels-using-sets-given-string/,"# Python3 code to count vowel in # a string using set # Function to count vowel def vowel_count(str): # Initializing count variable to 0 count = 0 # Creating a set of vowels vowel = set(""aeiouAEIOU"") # Loop to traverse the alphabet # in the given string for alphabet in str: # If alphabet is present # in set vowel if alphabet in vowel: count = count + 1 print(""No. of vowels :"", count) # Driver code str = ""GeeksforGeeks"" # Function Call vowel_count(str)","#Input : GeeksforGeeks #Output : No. of vowels : 5","Python program to count number of vowels using sets in given string # Python3 code to count vowel in # a string using set # Function to count vowel def vowel_count(str): # Initializing count variable to 0 count = 0 # Creating a set of vowels vowel = set(""aeiouAEIOU"") # Loop to traverse the alphabet # in the given string for alphabet in str: # If alphabet is present # in set vowel if alphabet in vowel: count = count + 1 print(""No. of vowels :"", count) # Driver code str = ""GeeksforGeeks"" # Function Call vowel_count(str) #Input : GeeksforGeeks #Output : No. of vowels : 5 [END]" Python program to count number of vowels using sets in given string,https://www.geeksforgeeks.org/python-program-count-number-vowels-using-sets-given-string/,"def vowel_count(str): # Creating a list of vowels vowels = ""aeiouAEIOU"" # Using list comprehension to count the number of vowels in the string count = len([char for char in str if char in vowels]) # Printing the count of vowels in the string print(""No. of vowels :"", count) # Driver code str = ""GeeksforGeeks"" # Function Call vowel_count(str)","#Input : GeeksforGeeks #Output : No. of vowels : 5","Python program to count number of vowels using sets in given string def vowel_count(str): # Creating a list of vowels vowels = ""aeiouAEIOU"" # Using list comprehension to count the number of vowels in the string count = len([char for char in str if char in vowels]) # Printing the count of vowels in the string print(""No. of vowels :"", count) # Driver code str = ""GeeksforGeeks"" # Function Call vowel_count(str) #Input : GeeksforGeeks #Output : No. of vowels : 5 [END]" Python Program to remove all duplicates from a given string,https://www.geeksforgeeks.org/remove-duplicates-given-string-python/,"from collections import OrderedDict # Function to remove all duplicates from string # and order does not matter def removeDupWithoutOrder(str): # set() --> A Set is an unordered collection # data type that is iterable, mutable, # and has no duplicate elements. # """".join() --> It joins two adjacent elements in # iterable with any symbol defined in # """" ( double quotes ) and returns a # single string return """".join(set(str)) # Function to remove all duplicates from string # and keep the order of characters same def removeDupWithOrder(str): return """".join(OrderedDict.fromkeys(str)) # Driver program if __name__ == ""__main__"": str = ""geeksforgeeks"" print(""Without Order = "", removeDupWithoutOrder(str)) print(""With Order = "", removeDupWithOrder(str))"," #Output : Without Order = foskerg","Python Program to remove all duplicates from a given string from collections import OrderedDict # Function to remove all duplicates from string # and order does not matter def removeDupWithoutOrder(str): # set() --> A Set is an unordered collection # data type that is iterable, mutable, # and has no duplicate elements. # """".join() --> It joins two adjacent elements in # iterable with any symbol defined in # """" ( double quotes ) and returns a # single string return """".join(set(str)) # Function to remove all duplicates from string # and keep the order of characters same def removeDupWithOrder(str): return """".join(OrderedDict.fromkeys(str)) # Driver program if __name__ == ""__main__"": str = ""geeksforgeeks"" print(""Without Order = "", removeDupWithoutOrder(str)) print(""With Order = "", removeDupWithOrder(str)) #Output : Without Order = foskerg [END]" Python Program to remove all duplicates from a given string,https://www.geeksforgeeks.org/remove-duplicates-given-string-python/,"def removeDuplicate(str): s = set(str) s = """".join(s) print(""Without Order:"", s) t = """" for i in str: if i in t: pass else: t = t + i print(""With Order:"", t) str = ""geeksforgeeks"" removeDuplicate(str)"," #Output : Without Order = foskerg","Python Program to remove all duplicates from a given string def removeDuplicate(str): s = set(str) s = """".join(s) print(""Without Order:"", s) t = """" for i in str: if i in t: pass else: t = t + i print(""With Order:"", t) str = ""geeksforgeeks"" removeDuplicate(str) #Output : Without Order = foskerg [END]" Python Program to remove all duplicates from a given string,https://www.geeksforgeeks.org/remove-duplicates-given-string-python/,"from collections import OrderedDict ordinary_dictionary = {} ordinary_dictionary[""a""] = 1 ordinary_dictionary[""b""] = 2 ordinary_dictionary[""c""] = 3 ordinary_dictionary[""d""] = 4 ordinary_dictionary[""e""] = 5 # Output = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} print(ordinary_dictionary) ordered_dictionary = OrderedDict() ordered_dictionary[""a""] = 1 ordered_dictionary[""b""] = 2 ordered_dictionary[""c""] = 3 ordered_dictionary[""d""] = 4 ordered_dictionary[""e""] = 5 # Output = {'a':1,'b':2,'c':3,'d':4,'e':5} print(ordered_dictionary)"," #Output : Without Order = foskerg","Python Program to remove all duplicates from a given string from collections import OrderedDict ordinary_dictionary = {} ordinary_dictionary[""a""] = 1 ordinary_dictionary[""b""] = 2 ordinary_dictionary[""c""] = 3 ordinary_dictionary[""d""] = 4 ordinary_dictionary[""e""] = 5 # Output = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} print(ordinary_dictionary) ordered_dictionary = OrderedDict() ordered_dictionary[""a""] = 1 ordered_dictionary[""b""] = 2 ordered_dictionary[""c""] = 3 ordered_dictionary[""d""] = 4 ordered_dictionary[""e""] = 5 # Output = {'a':1,'b':2,'c':3,'d':4,'e':5} print(ordered_dictionary) #Output : Without Order = foskerg [END]" Python Program to remove all duplicates from a given string,https://www.geeksforgeeks.org/remove-duplicates-given-string-python/,"from collections import OrderedDict seq = (""name"", ""age"", ""gender"") dict = OrderedDict.fromkeys(seq) # Output = {'age': None, 'name': None, 'gender': None} print(str(dict)) dict = OrderedDict.fromkeys(seq, 10) # Output = {'age': 10, 'name': 10, 'gender': 10} print(str(dict))"," #Output : Without Order = foskerg","Python Program to remove all duplicates from a given string from collections import OrderedDict seq = (""name"", ""age"", ""gender"") dict = OrderedDict.fromkeys(seq) # Output = {'age': None, 'name': None, 'gender': None} print(str(dict)) dict = OrderedDict.fromkeys(seq, 10) # Output = {'age': 10, 'name': 10, 'gender': 10} print(str(dict)) #Output : Without Order = foskerg [END]" Python Program to remove all duplicates from a given string,https://www.geeksforgeeks.org/remove-duplicates-given-string-python/,"import operator as op def removeDuplicate(str): s = set(str) s = """".join(s) print(""Without Order:"", s) t = """" for i in str: if op.countOf(t, i) > 0: pass else: t = t + i print(""With Order:"", t) str = ""geeksforgeeks"" removeDuplicate(str)"," #Output : Without Order = foskerg","Python Program to remove all duplicates from a given string import operator as op def removeDuplicate(str): s = set(str) s = """".join(s) print(""Without Order:"", s) t = """" for i in str: if op.countOf(t, i) > 0: pass else: t = t + i print(""With Order:"", t) str = ""geeksforgeeks"" removeDuplicate(str) #Output : Without Order = foskerg [END]" Python - Least Frequent Character in String,https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Least Frequent Character in String # naive method # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using naive method to get # Least Frequent Character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = min(all_freq, key=all_freq.get) # printing result print(""The minimum of all characters in GeeksforGeeks is : "" + str(res))"," #Output : The original string is : GeeksforGeeks","Python - Least Frequent Character in String # Python 3 code to demonstrate # Least Frequent Character in String # naive method # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using naive method to get # Least Frequent Character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = min(all_freq, key=all_freq.get) # printing result print(""The minimum of all characters in GeeksforGeeks is : "" + str(res)) #Output : The original string is : GeeksforGeeks [END]" Python - Least Frequent Character in String,https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Least Frequent Character in String # collections.Counter() + min() from collections import Counter # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using collections.Counter() + min() to get # Least Frequent Character in String res = Counter(test_str) res = min(res, key=res.get) # printing result print(""The minimum of all characters in GeeksforGeeks is : "" + str(res))"," #Output : The original string is : GeeksforGeeks","Python - Least Frequent Character in String # Python 3 code to demonstrate # Least Frequent Character in String # collections.Counter() + min() from collections import Counter # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using collections.Counter() + min() to get # Least Frequent Character in String res = Counter(test_str) res = min(res, key=res.get) # printing result print(""The minimum of all characters in GeeksforGeeks is : "" + str(res)) #Output : The original string is : GeeksforGeeks [END]" Python - Least Frequent Character in String,https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar,"from collections import defaultdict def least_frequent_char(string): freq = defaultdict(int) for char in string: freq[char] += 1 min_freq = min(freq.values()) least_frequent_chars = [char for char in freq if freq[char] == min_freq] return """".join(sorted(least_frequent_chars))[0] # Example usage test_str = ""GeeksorfGeeks"" least_frequent = least_frequent_char(test_str) print(f""The least frequent character in '{test_str}' is: {least_frequent}"")"," #Output : The original string is : GeeksforGeeks","Python - Least Frequent Character in String from collections import defaultdict def least_frequent_char(string): freq = defaultdict(int) for char in string: freq[char] += 1 min_freq = min(freq.values()) least_frequent_chars = [char for char in freq if freq[char] == min_freq] return """".join(sorted(least_frequent_chars))[0] # Example usage test_str = ""GeeksorfGeeks"" least_frequent = least_frequent_char(test_str) print(f""The least frequent character in '{test_str}' is: {least_frequent}"") #Output : The original string is : GeeksforGeeks [END]" Python - Least Frequent Character in String,https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar,"import numpy as np def least_frequent_char(string): freq = {char: string.count(char) for char in set(string)} return list(freq.keys())[np.argmin(list(freq.values()))] # Example usage input_string = ""GeeksforGeeks"" min_char = least_frequent_char(input_string) print(""The original string is:"", input_string) print(""The minimum of all characters in"", input_string, ""is:"", min_char) # This code is contributed by Jyothi Pinjala."," #Output : The original string is : GeeksforGeeks","Python - Least Frequent Character in String import numpy as np def least_frequent_char(string): freq = {char: string.count(char) for char in set(string)} return list(freq.keys())[np.argmin(list(freq.values()))] # Example usage input_string = ""GeeksforGeeks"" min_char = least_frequent_char(input_string) print(""The original string is:"", input_string) print(""The minimum of all characters in"", input_string, ""is:"", min_char) # This code is contributed by Jyothi Pinjala. #Output : The original string is : GeeksforGeeks [END]" Python - Least Frequent Character in String,https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar,"# Python program for the above approach # Function to find the least frequent characters def least_frequent_char(input_str): freq_dict = {} for char in input_str: freq_dict[char] = freq_dict.get(char, 0) + 1 min_value = min(freq_dict.values()) least_frequent_char = """" # Traversing the dictionary for key, value in freq_dict.items(): if value == min_value: least_frequent_char = key break return least_frequent_char # Driver Code input_str = ""geeksforgeeks"" print(least_frequent_char(input_str))"," #Output : The original string is : GeeksforGeeks","Python - Least Frequent Character in String # Python program for the above approach # Function to find the least frequent characters def least_frequent_char(input_str): freq_dict = {} for char in input_str: freq_dict[char] = freq_dict.get(char, 0) + 1 min_value = min(freq_dict.values()) least_frequent_char = """" # Traversing the dictionary for key, value in freq_dict.items(): if value == min_value: least_frequent_char = key break return least_frequent_char # Driver Code input_str = ""geeksforgeeks"" print(least_frequent_char(input_str)) #Output : The original string is : GeeksforGeeks [END]" Python | Maximum frequency character in String,https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Maximum frequency character in String # naive method # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using naive method to get # Maximum frequency character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = max(all_freq, key=all_freq.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(res))"," #Output : The original string is : GeeksforGeeks","Python | Maximum frequency character in String # Python 3 code to demonstrate # Maximum frequency character in String # naive method # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using naive method to get # Maximum frequency character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = max(all_freq, key=all_freq.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(res)) #Output : The original string is : GeeksforGeeks [END]" Python | Maximum frequency character in String,https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar,"# Python 3 code to demonstrate # Maximum frequency character in String # collections.Counter() + max() from collections import Counter # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using collections.Counter() + max() to get # Maximum frequency character in String res = Counter(test_str) res = max(res, key=res.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(res))"," #Output : The original string is : GeeksforGeeks","Python | Maximum frequency character in String # Python 3 code to demonstrate # Maximum frequency character in String # collections.Counter() + max() from collections import Counter # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # using collections.Counter() + max() to get # Maximum frequency character in String res = Counter(test_str) res = max(res, key=res.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(res)) #Output : The original string is : GeeksforGeeks [END]" Python | Maximum frequency character in String,https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar,"# initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # creating an empty dictionary freq = {} # counting frequency of each character for char in test_str: if char in freq: freq[char] += 1 else: freq[char] = 1 # finding the character with maximum frequency max_char = max(freq, key=freq.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(max_char))"," #Output : The original string is : GeeksforGeeks","Python | Maximum frequency character in String # initializing string test_str = ""GeeksforGeeks"" # printing original string print(""The original string is : "" + test_str) # creating an empty dictionary freq = {} # counting frequency of each character for char in test_str: if char in freq: freq[char] += 1 else: freq[char] = 1 # finding the character with maximum frequency max_char = max(freq, key=freq.get) # printing result print(""The maximum of all characters in GeeksforGeeks is : "" + str(max_char)) #Output : The original string is : GeeksforGeeks [END]" Python | Maximum frequency character in String,https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar,"test_str = ""GeeksforGeeks"" res = max(test_str, key=lambda x: test_str.count(x)) print(res)"," #Output : The original string is : GeeksforGeeks","Python | Maximum frequency character in String test_str = ""GeeksforGeeks"" res = max(test_str, key=lambda x: test_str.count(x)) print(res) #Output : The original string is : GeeksforGeeks [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + defaultdict() from collections import defaultdict # helper_function def hlper_fnc(test_str): cntr = defaultdict(int) for ele in test_str: cntr[ele] += 1 return [val for val, chr in cntr.items() if chr % 2 != 0] # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters # Using list comprehension + defaultdict() res = hlper_fnc(test_str) # printing result print(""The Odd Frequency Characters are :"" + str(res))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + defaultdict() from collections import defaultdict # helper_function def hlper_fnc(test_str): cntr = defaultdict(int) for ele in test_str: cntr[ele] += 1 return [val for val, chr in cntr.items() if chr % 2 != 0] # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters # Using list comprehension + defaultdict() res = hlper_fnc(test_str) # printing result print(""The Odd Frequency Characters are :"" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + Counter() from collections import Counter # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters # Using list comprehension + Counter() res = [chr for chr, count in Counter(test_str).items() if count & 1] # printing result print(""The Odd Frequency Characters are : "" + str(res))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + Counter() from collections import Counter # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters # Using list comprehension + Counter() res = [chr for chr, count in Counter(test_str).items() if count & 1] # printing result print(""The Odd Frequency Characters are : "" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Odd Frequency Characters # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if test_str.count(i) % 2 != 0: res.append(i) # printing result print(""The Odd Frequency Characters are : "" + str(res))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if test_str.count(i) % 2 != 0: res.append(i) # printing result print(""The Odd Frequency Characters are : "" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of # Odd Frequency Characters import operator as op # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if op.countOf(test_str, i) % 2 != 0: res.append(i) # printing result print(""The Odd Frequency Characters are : "" + str(res))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters import operator as op # initializing string test_str = ""geekforgeeks is best for geeks"" # printing original string print(""The original string is : "" + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if op.countOf(test_str, i) % 2 != 0: res.append(i) # printing result print(""The Odd Frequency Characters are : "" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"def odd_freq_chars(test_str): # Create an empty dictionary to hold character counts char_counts = {} # Loop through each character in the input string for char in test_str: # Increment the count for this character in the dictionary # using the get() method to safely handle uninitialized keys char_counts[char] = char_counts.get(char, 0) + 1 # Create a list of characters whose counts are odd return [char for char, count in char_counts.items() if count % 2 != 0] # Test the function with sample input test_str = ""geekforgeeks is best for geeks"" print(""The original string is : "" + str(test_str)) res = odd_freq_chars(test_str) print(""The Odd Frequency Characters are :"" + str(res))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character def odd_freq_chars(test_str): # Create an empty dictionary to hold character counts char_counts = {} # Loop through each character in the input string for char in test_str: # Increment the count for this character in the dictionary # using the get() method to safely handle uninitialized keys char_counts[char] = char_counts.get(char, 0) + 1 # Create a list of characters whose counts are odd return [char for char, count in char_counts.items() if count % 2 != 0] # Test the function with sample input test_str = ""geekforgeeks is best for geeks"" print(""The original string is : "" + str(test_str)) res = odd_freq_chars(test_str) print(""The Odd Frequency Characters are :"" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Odd Frequency Character,https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar,"# Python3 code to demonstrate working of# Odd Frequency Characters??????from collections import defaultdict, Counter??????# initializing stringtest_str = 'geekforgeeks is best for geeks'??????# printing original ""The original string is : "" + str(test_str))??????# Odd Frequency Characters using defaultdictchar_count = defaultdict(int)for c in test_str:????????????????????????char_count += 1??????odd_freq_chars = % 2 != 0]?""The Odd Frequency Characters are : "" + str(odd_freq_chars))??????# Odd Frequency Characters using Counterchar_count = Counter(test_str)??????odd_freq_chars = % 2 != 0]??????# printing ""The Odd Frequency Characters are : "" + str(odd_freq_chars))","#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's","Python - Odd Frequency Character # Python3 code to demonstrate working of# Odd Frequency Characters??????from collections import defaultdict, Counter??????# initializing stringtest_str = 'geekforgeeks is best for geeks'??????# printing original ""The original string is : "" + str(test_str))??????# Odd Frequency Characters using defaultdictchar_count = defaultdict(int)for c in test_str:????????????????????????char_count += 1??????odd_freq_chars = % 2 != 0]?""The Odd Frequency Characters are : "" + str(odd_freq_chars))??????# Odd Frequency Characters using Counterchar_count = Counter(test_str)??????odd_freq_chars = % 2 != 0]??????# printing ""The Odd Frequency Characters are : "" + str(odd_freq_chars)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]" Python - Specific Characters Frequency in String List,https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/,"# Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using join() + Counter() from collections import Counter # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # dict comprehension to retrieve on certain Frequencies res = { key: val for key, val in dict(Counter("""".join(test_list))).items() if key in chr_list } # printing result print(""Specific Characters Frequencies : "" + str(res))"," #Output : The original list : ['geeksforgeeks is best for geeks']","Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using join() + Counter() from collections import Counter # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # dict comprehension to retrieve on certain Frequencies res = { key: val for key, val in dict(Counter("""".join(test_list))).items() if key in chr_list } # printing result print(""Specific Characters Frequencies : "" + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]" Python - Specific Characters Frequency in String List,https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/,"# Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using chain.from_iterable() + Counter() + dictionary comprehension from collections import Counter from itertools import chain # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # dict comprehension to retrieve on certain Frequencies # from_iterable to flatten / join res = { key: val for key, val in dict(Counter(chain.from_iterable(test_list))).items() if key in chr_list } # printing result print(""Specific Characters Frequencies : "" + str(res))"," #Output : The original list : ['geeksforgeeks is best for geeks']","Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using chain.from_iterable() + Counter() + dictionary comprehension from collections import Counter from itertools import chain # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # dict comprehension to retrieve on certain Frequencies # from_iterable to flatten / join res = { key: val for key, val in dict(Counter(chain.from_iterable(test_list))).items() if key in chr_list } # printing result print(""Specific Characters Frequencies : "" + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]" Python - Specific Characters Frequency in String List,https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/,"# Python3 code to demonstrate working of # Specific Characters Frequency in String List # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] d = dict() for i in chr_list: d[i] = test_list[0].count(i) res = d # printing result print(""Specific Characters Frequencies : "" + str(res))"," #Output : The original list : ['geeksforgeeks is best for geeks']","Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] d = dict() for i in chr_list: d[i] = test_list[0].count(i) res = d # printing result print(""Specific Characters Frequencies : "" + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]" Python - Specific Characters Frequency in String List,https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/,"# Python3 code to demonstrate working of # Specific Characters Frequency in String List import operator as op # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] d = dict() for i in chr_list: d[i] = op.countOf(test_list[0], i) res = d # printing result print(""Specific Characters Frequencies : "" + str(res))"," #Output : The original list : ['geeksforgeeks is best for geeks']","Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List import operator as op # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] d = dict() for i in chr_list: d[i] = op.countOf(test_list[0], i) res = d # printing result print(""Specific Characters Frequencies : "" + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]" Python - Specific Characters Frequency in String List,https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/,"# initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # initializing dictionary for result res = {} # loop through each character in the test_list and count their frequency for char in """".join(test_list): if char in chr_list: if char in res: res[char] += 1 else: res[char] = 1 # printing result print(""Specific Characters Frequencies : "" + str(res))"," #Output : The original list : ['geeksforgeeks is best for geeks']","Python - Specific Characters Frequency in String List # initializing lists test_list = [""geeksforgeeks is best for geeks""] # printing original list print(""The original list : "" + str(test_list)) # char list chr_list = [""e"", ""b"", ""g""] # initializing dictionary for result res = {} # loop through each character in the test_list and count their frequency for char in """".join(test_list): if char in chr_list: if char in res: res[char] += 1 else: res[char] = 1 # printing result print(""Specific Characters Frequencies : "" + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + len() import re # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String # Using re.findall() + len() res = len(re.findall(r""\d+"", test_str)) # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + len() import re # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String # Using re.findall() + len() res = len(re.findall(r""\d+"", test_str)) # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + sum() import re # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String # Using re.findall() + sum() res = sum(1 for _ in re.finditer(r""\d+"", test_str)) # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + sum() import re # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String # Using re.findall() + sum() res = sum(1 for _ in re.finditer(r""\d+"", test_str)) # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = 0 for i in test_str: if i.isdigit(): res += 1 # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = 0 for i in test_str: if i.isdigit(): res += 1 # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = 0 digits = ""0123456789"" for i in test_str: if i in digits: res += 1 # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = 0 digits = ""0123456789"" for i in test_str: if i in digits: res += 1 # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = len(list(filter(lambda x: x.isdigit(), test_str))) # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = len(list(filter(lambda x: x.isdigit(), test_str))) # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Frequency of numbers in String,https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/,"# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = sum(map(str.isdigit, test_str)) # printing result print(""Count of numerics in string : "" + str(res))"," #Output : The original string is : geeks4feeks is No. 1 4 geeks","Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = ""geeks4feeks is No. 1 4 geeks"" # printing original string print(""The original string is : "" + test_str) # Frequency of numbers in String res = sum(map(str.isdigit, test_str)) # printing result print(""Count of numerics in string : "" + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"// C++ program to check if a string// contains any special character??????// import required packages#include #include using namespace std;??????// Function checks if the string// contains any special charactervoid run(string str){??????????????????????????????????????????????????????""[@_!#$%^&*()<>?/|}{~:]"");??????????????????????????????// Pass the string in regex_search????????????????????????// method????????????????????????if(r""String is accepted"";????????????????????????else????????""String is not accepted."";}??????// Driver Codeint main(){??????????????????????????????????????????????????????""Geeks$For$Geeks"";??????????????????????????????????????????????????????// Calling run function????????????????????????run(str);???????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character // C++ program to check if a string// contains any special character??????// import required packages#include #include using namespace std;??????// Function checks if the string// contains any special charactervoid run(string str){??????????????????????????????????????????????????????""[@_!#$%^&*()<>?/|}{~:]"");??????????????????????????????// Pass the string in regex_search????????????????????????// method????????????????????????if(r""String is accepted"";????????????????????????else????????""String is not accepted."";}??????// Driver Codeint main(){??????????????????????????????????????????????????????""Geeks$For$Geeks"";??????????????????????????????????????????????????????// Calling run function????????????????????????run(str);??????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"# Python3 program to check if a string # contains any special character # import required package import re # Function checks if the string # contains any special character def run(string): # Make own character set and pass # this as argument in compile method regex = re.compile(""[@_!#$%^&*()<>?/\|}{~:]"") # Pass the string in search # method of regex object. if regex.search(string) == None: print(""String is accepted"") else: print(""String is not accepted."") # Driver Code if __name__ == ""__main__"": # Enter the string string = ""Geeks$For$Geeks"" # calling run function run(string)","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character # Python3 program to check if a string # contains any special character # import required package import re # Function checks if the string # contains any special character def run(string): # Make own character set and pass # this as argument in compile method regex = re.compile(""[@_!#$%^&*()<>?/\|}{~:]"") # Pass the string in search # method of regex object. if regex.search(string) == None: print(""String is accepted"") else: print(""String is not accepted."") # Driver Code if __name__ == ""__main__"": # Enter the string string = ""Geeks$For$Geeks"" # calling run function run(string) #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"?/\|}{~:]',???????????????????????????????????????????????????????????????????????????????????????????????????????????????""String is accepted"");????????????????????????????????????????????????????""String is not accepted."");}??????// Driver Code??????// Enter the string$string = 'Geeks$For$Geeks';??????// calling run functionrun($string);??????// This code is contribute","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character ?/\|}{~:]',???????????????????????????????????????????????????????????????????????????????????????????????????????????????""String is accepted"");????????????????????????????????????????????????????""String is not accepted."");}??????// Driver Code??????// Enter the string$string = 'Geeks$For$Geeks';??????// calling run functionrun($string);??????// This code is contribute #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"import java.util.regex.Matcher;import java.util.regex.Pattern;??????public class Main {????????????????????????public static void main(String[] args)??????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????Pattern pattern??????????""[@_!#$%^&*()<>?/|}{~:]"");????????????????????????????????????????????????Matcher matcher = pattern.matcher(str);???????????????????????????????????????????????????""String is accepted"");????????????????????????????????????????????????}?????????????????????????????????""String is not accepted"");????????????????????????????????????????????????}????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character import java.util.regex.Matcher;import java.util.regex.Pattern;??????public class Main {????????????????????????public static void main(String[] args)??????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????Pattern pattern??????????""[@_!#$%^&*()<>?/|}{~:]"");????????????????????????????????????????????????Matcher matcher = pattern.matcher(str);???????????????????????????????????????????????????""String is accepted"");????????????????????????????????????????????????}?????????????????????????????????""String is not accepted"");????????????????????????????????????????????????}???????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"// C# program to check if a string// contains any special character??????using System;using System.Text.RegularExpressions;??????class Program {????????????????????????// Function checks if the string????????????????????????// contains any special character????????????????????????static void Run(string str)????????????????????????{?????????""[@_!#$%^&*()<>?/|}{~:]"");????????????????????????????????????????????????// Pass the string in regex.IsMatch????????????????????????????????????????????????// method??????????????????""String is accepted"");????????????????????????????????????????????????else??????????""String is not accepted."");????????????????????????}??????????????????????????????// Driver Code????????????????????????static void Main()?????????????????????""Geeks$For$Geeks"";??????????????????????????????????????????????????????// Calling Run function????????????????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character // C# program to check if a string// contains any special character??????using System;using System.Text.RegularExpressions;??????class Program {????????????????????????// Function checks if the string????????????????????????// contains any special character????????????????????????static void Run(string str)????????????????????????{?????????""[@_!#$%^&*()<>?/|}{~:]"");????????????????????????????????????????????????// Pass the string in regex.IsMatch????????????????????????????????????????????????// method??????????????????""String is accepted"");????????????????????????????????????????????????else??????????""String is not accepted."");????????????????????????}??????????????????????????????// Driver Code????????????????????????static void Main()?????????????????????""Geeks$For$Geeks"";??????????????????????????????????????????????????????// Calling Run function???????????????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"function hasSpecialChar(str) {????????????let regex = /[@!#$%^&*()_+\-=\""\\|,.<>\/?]/;????????????return regex.test(str);}????""Geeks$For$Geeks"";if (!hasSpecialChar(str)) {????????????cons""String is accepted"");} else {????????????cons""String is not accepted"");}","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character function hasSpecialChar(str) {????????????let regex = /[@!#$%^&*()_+\-=\""\\|,.<>\/?]/;????????????return regex.test(str);}????""Geeks$For$Geeks"";if (!hasSpecialChar(str)) {????????????cons""String is accepted"");} else {????????????cons""String is not accepted"");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"// C++ code// to check if any special character is present// in a given string or not#include #include using namespace std;??????int main(){??????????????????????????????// Input s""Geeks$For$Geeks"";????????????????????????int c = 0;??????????????????""[@_!#$%^&*()<>?}{~:]""; // special character set????????????????????????for (int i = 0; i < n.size(); i++) {??????????????????????????????????????????????????????// checking if any special character is present in????????????????????????????????????????????????// given string or not????????????????????????????????????????????????if (s.find(n[i]) != string::npos) {????????????????????????????????????????????????????????????????????????// if special character found then add 1 to the??????????????????????????????????????????????????????????????????""string is not accepted"";????????????????????????}??????????????????????""string is accepted"";????????????????????????}??????????????????????????????return 0;}??????/","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character // C++ code// to check if any special character is present// in a given string or not#include #include using namespace std;??????int main(){??????????????????????????????// Input s""Geeks$For$Geeks"";????????????????????????int c = 0;??????????????????""[@_!#$%^&*()<>?}{~:]""; // special character set????????????????????????for (int i = 0; i < n.size(); i++) {??????????????????????????????????????????????????????// checking if any special character is present in????????????????????????????????????????????????// given string or not????????????????????????????????????????????????if (s.find(n[i]) != string::npos) {????????????????????????????????????????????????????????????????????????// if special character found then add 1 to the??????????????????????????????????????????????????????????????????""string is not accepted"";????????????????????????}??????????????????????""string is accepted"";????????????????????????}??????????????????????????????return 0;}??????/ #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"// Java code to check if any special character is present// in a given string or notimport java.util.*;class Main {????????????????????????public static void main(String[] args)????????????????????????{??????????????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????int c""[@_!#$%^&*()<>?}{~:]""; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set????????????????????????????????????????????????for (int i = 0; i < n.length(); i++) {??????????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.indexOf(n.charAt(i)) != -1) {????????????????????????????????????""string is not accepted"");????????????????????????????????????????????????}?????????????????????????????????""string is accepted"");???????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character // Java code to check if any special character is present// in a given string or notimport java.util.*;class Main {????????????????????????public static void main(String[] args)????????????????????????{??????????????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????int c""[@_!#$%^&*()<>?}{~:]""; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set????????????????????????????????????????????????for (int i = 0; i < n.length(); i++) {??????????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.indexOf(n.charAt(i)) != -1) {????????????????????????????????????""string is not accepted"");????????????????????????????????????????????????}?????????????????????????????????""string is accepted"");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"# Python code # to check if any special character is present # in a given string or not # input string n = ""Geeks$For$Geeks"" n.split() c = 0 s = ""[@_!#$%^&*()<>?/\|}{~:]"" # special character set for i in range(len(n)): # checking if any special character is present in given string or not if n[i] in s: c += 1 # if special character found then add 1 to the c # if c value is greater than 0 then print no # means special character is found in string if c: print(""string is not accepted"") else: print(""string accepted"") # this code is contributed by gangarajula laxmi","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character # Python code # to check if any special character is present # in a given string or not # input string n = ""Geeks$For$Geeks"" n.split() c = 0 s = ""[@_!#$%^&*()<>?/\|}{~:]"" # special character set for i in range(len(n)): # checking if any special character is present in given string or not if n[i] in s: c += 1 # if special character found then add 1 to the c # if c value is greater than 0 then print no # means special character is found in string if c: print(""string is not accepted"") else: print(""string accepted"") # this code is contributed by gangarajula laxmi #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"// JavaScript code// to check if any special character is present// in a given string or notconst n = ""Geeks$For$Geeks"";let c = 0;const s = ""[@_!#$%^&*()<>?}{~:]""; // special character set??????for (let i = 0; i < n.length; i++) {????????????????????????// checking if any special character is present in given string or not????????????????????????if (s.includes(n[i])) {????????????????????????????????????????????????// if special character found then add 1 to the c????????????????????????????????????????????????c++;????????????????????????}}????""string is not accepted"");} else {????????????????????""string is accepted"");}","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character // JavaScript code// to check if any special character is present// in a given string or notconst n = ""Geeks$For$Geeks"";let c = 0;const s = ""[@_!#$%^&*()<>?}{~:]""; // special character set??????for (let i = 0; i < n.length; i++) {????????????????????????// checking if any special character is present in given string or not????????????????????????if (s.includes(n[i])) {????????????????????????????????????????????????// if special character found then add 1 to the c????????????????????????????????????????????????c++;????????????????????????}}????""string is not accepted"");} else {????????????????????""string is accepted"");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"using System;??????class Program {????????????????????????static void Main(string[] args)????????????????????????{??????????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????int c""[@_!#$%^&*()<>?}{~:]""; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set??????????????????????????????????????????????????????for (int i = 0; i < n.Length; i++) {????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.IndexOf(n[i]) != -1) {???????????????????????????????????""string is not accepted"");????????????????????????????????????????????????}????????????????????????????????""string is accepted"");???????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character using System;??????class Program {????????????????????????static void Main(string[] args)????????????????????????{??????????????????????""Geeks$For$Geeks"";????????????????????????????????????????????????int c""[@_!#$%^&*()<>?}{~:]""; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set??????????????????????????????????????????????????????for (int i = 0; i < n.Length; i++) {????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.IndexOf(n[i]) != -1) {???????????????????????????????????""string is not accepted"");????????????????????????????????????????????????}????????????????????????????????""string is accepted"");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"#include #include ??????using namespace std;??????bool hasSpecialChar(string s){????????????????????????for (char c : s) {????????????????????????????????????????????????if (!(isalpha(c) || isdigit(c) || c == ' ')) {????????????????????????????????????????????????????????????""Hello World"";????????????????????????if (hasSpecialChar(s)) {????????""The string contains special characters.""??????????????????????????<< endl;????????}????????else {????????????????cout << ""The string does not contain special ""????????????????????????????????""characters.""??????????????????????????<< endl;????????}??????????s = ""Hello@World"";????????????????????????if (hasSpecialChar(s)) {????????""The string contains special characters.""??????????????????????????<< endl;????????}????????else {????????????????cout << ""The string does not contain special ""????????????????????????????????""characters.""??????????????????????????<< endl;????????}??????????return 0;}","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character #include #include ??????using namespace std;??????bool hasSpecialChar(string s){????????????????????????for (char c : s) {????????????????????????????????????????????????if (!(isalpha(c) || isdigit(c) || c == ' ')) {????????????????????????????????????????????????????????????""Hello World"";????????????????????????if (hasSpecialChar(s)) {????????""The string contains special characters.""??????????????????????????<< endl;????????}????????else {????????????????cout << ""The string does not contain special ""????????????????????????????????""characters.""??????????????????????????<< endl;????????}??????????s = ""Hello@World"";????????????????????????if (hasSpecialChar(s)) {????????""The string contains special characters.""??????????????????????????<< endl;????????}????????else {????????????????cout << ""The string does not contain special ""????????????????????????????????""characters.""??????????????????????????<< endl;????????}??????????return 0;} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"class Main {????????????????????????public static boolean hasSpecialChar(String s)????????????????????????{????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????char c = s.charAt(i);????????????????????????????????????????????????????????????????????????if (!(Character.isLetterOrDigit(c)????????????????????????????????????????????????????????????????????????????????????????????????????""Hello World"";????????????????????????????????????????????????if (hasSpecialChar(s1)) {???????????????????????????????????????????""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????""The string does not contain special characters."");?????????????????????????????????????????????""Hello@World"";????????????????????????????????????????????????if (hasSpecialChar(s2)) {???????????????????????????????????????????""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????""The string does not contain special characters."");???????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character class Main {????????????????????????public static boolean hasSpecialChar(String s)????????????????????????{????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????char c = s.charAt(i);????????????????????????????????????????????????????????????????????????if (!(Character.isLetterOrDigit(c)????????????????????????????????????????????????????????????????????????????????????????????????????""Hello World"";????????????????????????????????????????????????if (hasSpecialChar(s1)) {???????????????????????????????????????????""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????""The string does not contain special characters."");?????????????????????????????????????????????""Hello@World"";????????????????????????????????????????????????if (hasSpecialChar(s2)) {???????????????????????????????????????????""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????""The string does not contain special characters."");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"def has_special_char(s): for c in s: if not (c.isalpha() or c.isdigit() or c == "" ""): return True return False # Test the function s = ""Hello World"" if has_special_char(s): print(""The string contains special characters."") else: print(""The string does not contain special characters."") s = ""Hello@World"" if has_special_char(s): print(""The string contains special characters."") else: print(""The string does not contain special characters."") # This code is contributed by Edula Vinay Kumar Reddy","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character def has_special_char(s): for c in s: if not (c.isalpha() or c.isdigit() or c == "" ""): return True return False # Test the function s = ""Hello World"" if has_special_char(s): print(""The string contains special characters."") else: print(""The string does not contain special characters."") s = ""Hello@World"" if has_special_char(s): print(""The string contains special characters."") else: print(""The string does not contain special characters."") # This code is contributed by Edula Vinay Kumar Reddy #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"using System;??????class GFG {????????????????????????// This method checks if a string contains any special????????????????????????// characters.????????????????????????public static bool HasSpecialChar(string s)????????????????????????{????????????????????????????????????????????????// Iterate over each character in the string.????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????char c = s[i];????????????????????????????????????????????????????????????????????????// If the character is not a letter, digit, or????????????????????????????????????????????????????????????????????????// space, it is a special character.????????????????????????????????????????????????????????????????????????if (!(char""Hello World"";????????????????????????????????????????????????// Check if s1 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if ""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????""The string does not contain special characters."");?????????????????????????????????????????????""Hello@World"";????????????????????????????????????????????????// Check if s2 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if ""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????""The string does not contain special characters."");???????????????????????????","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character using System;??????class GFG {????????????????????????// This method checks if a string contains any special????????????????????????// characters.????????????????????????public static bool HasSpecialChar(string s)????????????????????????{????????????????????????????????????????????????// Iterate over each character in the string.????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????char c = s[i];????????????????????????????????????????????????????????????????????????// If the character is not a letter, digit, or????????????????????????????????????????????????????????????????????????// space, it is a special character.????????????????????????????????????????????????????????????????????????if (!(char""Hello World"";????????????????????????????????????????????????// Check if s1 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if ""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????""The string does not contain special characters."");?????????????????????????????????????????????""Hello@World"";????????????????????????????????????????????????// Check if s2 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if ""The string contains special characters."");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????""The string does not contain special characters."");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"function hasSpecialChar(s) {????????????????????????for (let i = 0; i < s.length; i++) {????????????????????????????????????????????????const c = s.charAt(i);????????????????????????????????????????????????if (!(c.match(/^[a-zA-Z0-9 ]+$/))) {??????????????""Hello World"";if (hasSpecialChar(s)) {????????????????????""The string contains special characters."");} else {????????????????????""The string does not contain special characters."");}??????""Hello@World"";if (hasSpecialChar(s)) {????????????????????""The string contains special characters."");} else {????????????????????""The string does not contain special characters."");}","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character function hasSpecialChar(s) {????????????????????????for (let i = 0; i < s.length; i++) {????????????????????????????????????????????????const c = s.charAt(i);????????????????????????????????????????????????if (!(c.match(/^[a-zA-Z0-9 ]+$/))) {??????????????""Hello World"";if (hasSpecialChar(s)) {????????????????????""The string contains special characters."");} else {????????????????????""The string does not contain special characters."");}??????""Hello@World"";if (hasSpecialChar(s)) {????????????????????""The string contains special characters."");} else {????????????????????""The string does not contain special characters."");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Python | Program to check if a string contains any special character,https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/,"import string def check_string(s): for c in s: if c in string.punctuation: print(""String is not accepted"") return print(""String is accepted"") # Example usage check_string(""Geeks$For$Geeks"") # Output: String is not accepted check_string(""Geeks For Geeks"") # Output: String is accepted","#Input : Geeks$For$Geeks #Output : String is not accepted.","Python | Program to check if a string contains any special character import string def check_string(s): for c in s: if c in string.punctuation: print(""String is not accepted"") return print(""String is accepted"") # Example usage check_string(""Geeks$For$Geeks"") # Output: String is not accepted check_string(""Geeks For Geeks"") # Output: String is accepted #Input : Geeks$For$Geeks #Output : String is not accepted. [END]" Generating random strings until a given string is generated,https://www.geeksforgeeks.org/python-program-match-string-random-strings-length/,"# Python program to generate and match??????# the string from all random strings# of same length????????????# Importing string, random# and time modulesimport stringimport randomimport time????????????# all possible characters including??????# lowercase, uppercase and special symbolspossibleCharacters = string.ascii_lowercase + string.digits +???????????????????????????????????????????????????????????????????????????????????????""geek""????# To take input from user# t = input(str(""Enter your target text: ""))????????????attemptThis = ''.join(random.choice(possibleCharacters)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????for i in range(len(t)))attemptNext = ''????????????completed = Falseiteration = 0????????????# Iterate while completed is falsewhile completed == False:????????????????????????print(attemptThis)????????????????????????????????????????????????????????????attemptNext = ''????????????????????????completed = True????????????????????????????????????????????????????????????# Fix the index if matches with??????????????????????????????# the strings to be generated????????????????????????for i in range(len(t)):????????????????????????????????????????????????if attemptThis[i] != t[i]:?????????""Target matched after "" +?????????????????????????????"" iterations"")","#Input : GFG ","Generating random strings until a given string is generated # Python program to generate and match??????# the string from all random strings# of same length????????????# Importing string, random# and time modulesimport stringimport randomimport time????????????# all possible characters including??????# lowercase, uppercase and special symbolspossibleCharacters = string.ascii_lowercase + string.digits +???????????????????????????????????????????????????????????????????????????????????????""geek""????# To take input from user# t = input(str(""Enter your target text: ""))????????????attemptThis = ''.join(random.choice(possibleCharacters)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????for i in range(len(t)))attemptNext = ''????????????completed = Falseiteration = 0????????????# Iterate while completed is falsewhile completed == False:????????????????????????print(attemptThis)????????????????????????????????????????????????????????????attemptNext = ''????????????????????????completed = True????????????????????????????????????????????????????????????# Fix the index if matches with??????????????????????????????# the strings to be generated????????????????????????for i in range(len(t)):????????????????????????????????????????????????if attemptThis[i] != t[i]:?????????""Target matched after "" +?????????????????????????????"" iterations"") #Input : GFG [END]" Find words which are greater than K than given length k,https://www.geeksforgeeks.org/find-words-greater-given-length-k/,"// C++ program to find all string// which are greater than given length k??????#include using namespace std;??????// function find string greater than// length kvoid string_k(string s, int k){????????????????????????// create an empty s"""";????????????????????????// iterate the loop till every space????????????????????????for (int i = 0; i < s.size(); i++) {????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????w = w + s[i];???????????????????????????????????????"" "";????????????????????????????"""";????????????????????????????????????????????????}????????????????????????""geek for geeks"";????????????????????????int k = 3;"" "";????????????????????????string_k(s, k);????????????????????????return 0;}??????// This code is contri","#Input : str = ""hello geeks for geeks is computer science portal"" ","Find words which are greater than K than given length k // C++ program to find all string// which are greater than given length k??????#include using namespace std;??????// function find string greater than// length kvoid string_k(string s, int k){????????????????????????// create an empty s"""";????????????????????????// iterate the loop till every space????????????????????????for (int i = 0; i < s.size(); i++) {????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????w = w + s[i];???????????????????????????????????????"" "";????????????????????????????"""";????????????????????????????????????????????????}????????????????????????""geek for geeks"";????????????????????????int k = 3;"" "";????????????????????????string_k(s, k);????????????????????????return 0;}??????// This code is contri #Input : str = ""hello geeks for geeks is computer science portal"" [END]" Find words which are greater than K than given length k,https://www.geeksforgeeks.org/find-words-greater-given-length-k/,"// C# program to find all string// which are greater than given length k??????using System;??????class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(string s, int k)??????????????????"""";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w"" "");????????????????????????????????????"""";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}???????????????""geek for geeks"";????????????????????????????????????????????????in"" "";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi","#Input : str = ""hello geeks for geeks is computer science portal"" ","Find words which are greater than K than given length k // C# program to find all string// which are greater than given length k??????using System;??????class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(string s, int k)??????????????????"""";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w"" "");????????????????????????????????????"""";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}???????????????""geek for geeks"";????????????????????????????????????????????????in"" "";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi #Input : str = ""hello geeks for geeks is computer science portal"" [END]" Find words which are greater than K than given length k,https://www.geeksforgeeks.org/find-words-greater-given-length-k/,"// Java program to find all string// which are greater than given length k??????import java.io.*;import java.util.*;??????public class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(String s, int k)??????????????????"""";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.charAt("" "");????????????????????????????????????"""";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}????????????????????????}??????????""geek for geeks"";????????????????????????????????????????????????in"" "";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi","#Input : str = ""hello geeks for geeks is computer science portal"" ","Find words which are greater than K than given length k // Java program to find all string// which are greater than given length k??????import java.io.*;import java.util.*;??????public class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(String s, int k)??????????????????"""";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.charAt("" "");????????????????????????????????????"""";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}????????????????????????}??????????""geek for geeks"";????????????????????????????????????????????????in"" "";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi #Input : str = ""hello geeks for geeks is computer science portal"" [END]" Find words which are greater than K than given length k,https://www.geeksforgeeks.org/find-words-greater-given-length-k/,"# Python program to find all string # which are greater than given length k # function find string greater than length k def string_k(k, str): # create the empty string string = [] # split the string where space is comes text = str.split("" "") # iterate the loop till every substring for x in text: # if length of current sub string # is greater than k then if len(x) > k: # append this sub string in # string list string.append(x) # return string list return string # Driver Program k = 3 str = ""geek for geeks"" print(string_k(k, str))","#Input : str = ""hello geeks for geeks is computer science portal"" ","Find words which are greater than K than given length k # Python program to find all string # which are greater than given length k # function find string greater than length k def string_k(k, str): # create the empty string string = [] # split the string where space is comes text = str.split("" "") # iterate the loop till every substring for x in text: # if length of current sub string # is greater than k then if len(x) > k: # append this sub string in # string list string.append(x) # return string list return string # Driver Program k = 3 str = ""geek for geeks"" print(string_k(k, str)) #Input : str = ""hello geeks for geeks is computer science portal"" [END]" Find words which are greater than K than given length k,https://www.geeksforgeeks.org/find-words-greater-given-length-k/,"// javascript program to find all string// which are greater than given length k??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????function string_k( s , k) {??????????????"""";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (i = 0; i < s.length; i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.cha"" "");????????????????????????????????????"""";????????????????????????????????????????????????????????????????????????}????????????????????????????????????""geek for geeks"";????????????????????????????????????????????????va"" "";????????????????????????????????????????????????string_k(s, k);??????// This code is ","#Input : str = ""hello geeks for geeks is computer science portal"" ","Find words which are greater than K than given length k ????????????????????????????????????