When it is required to print the elements which are multiples of elements given in a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [45, 67, 89, 90, 10, 98, 10, 12, 23] print("The list is :") print(my_list) my_division_list = [6, 4] print("The division list is :") print(my_division_list) my_result = [element for element in my_list if all(element % j == 0 for j in my_division_list)] print("The result is :") print(my_result)OutputThe list is : [45, 67, 89, 90, 10, 98, 10, 12, 23] The division list is : [6, 4] The result ... Read More
When it is required to print strings based on the list of prefix elements, a list comprehension, the ‘any’ operator and the ‘startswith’ method are used.ExampleBelow is a demonstration of the samemy_list = ["streek", "greet", "meet", "leeks", "mean"] print("The list is : ") print(my_list) prefix_list = ["st", "ge", "me", "re"] print("The prefix list is : ") print(prefix_list) my_result = [element for element in my_list if any(element.startswith(ele) for ele in prefix_list)] print("The result is :") print(my_result)OutputThe list is : ['streek', 'greet', 'meet', 'leeks', 'mean'] The prefix list is : ['st', 'ge', 'me', 're'] The result is : ... Read More
When it is required to remove row with custom list element, a list comprehension and the ‘any’ operator are used.ExampleBelow is a demonstration of the samemy_list = [[14, 3, 11], [66, 27, 8], [31, 12, 21], [11, 16, 26]] print("The list is :") print(my_list) check_list = [3, 10, 19, 29, 20, 15] print("The check list is :") print(check_list) my_result = [row for row in my_list if not any(element in row for element in check_list)] print("The result is :") print(my_result)OutputThe list is : [[14, 3, 11], [66, 27, 8], [31, 12, 21], [11, 16, 26]] The check ... Read More
When it is required to remove duplicate elements index from other list, the ‘enumerate’ attribute, the list comprehension and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list_1 = [4, 5, 6, 5, 4, 7, 8, 6] my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) temp_set = set() temp = [] for index, value in enumerate(my_list_1): if value not in temp_set: temp_set.add(value) else: ... Read More
When it is required to remove specific digit from every element of the list, an iteration and ‘set’ operator and ‘str’ methods are used.ExampleBelow is a demonstration of the samemy_list = [123, 565, 1948, 334, 4598] print("The list is :") print(my_list) key = 3 print("The key is :") print(key) my_result = [] for element in my_list: if list(set(str(element)))[0] == str(key) and len(set(str(element))) == 1: my_result.append('') else: my_result.append(int(''.join([element_1 for element_1 in str(element) if int(element_1) != key]))) print("The result ... Read More
To display only the column names in a DataFrame, use dataframe.columns.Import the required library with an alias −import pandas as pd;Following is the DataFrame −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', 'Mumbai', 'Jaipur'], "Units": [100, 150, 50, 110, 90, 120, 80] } )ExampleDisplay the column names with dataframe.columns. Following is the complete code −import pandas as pd; # create a DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', ... Read More
When it is required to check wether all elements in a list of strings are numeric, the ‘all’ operator is used.ExampleBelow is a demonstration of the samemy_list = ["434", "823", "98", "74", '9870'] print("The list is :") print(my_list) my_result = all(ele.isdigit() for ele in my_list) if(my_result == True): print("All the elements in the list are numeric") else: print("All the elements in the list are not numeric")OutputThe list is : ['434', '823', '98', '74', '9870'] All the elements in the list are numericExplanationA list of integers is defined and is displayed on ... Read More
When it is required to replace elements of a list based on the comparison with a number, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [32, 37, 44, 38, 92, 61, 28, 92, 20] print("The list is :") print(my_list) my_key = 32 print("The key is ") print(my_key) low, high = 2, 9 my_result = [] for ele in my_list: if ele > my_key: my_result.append(high) else: my_result.append(low) print("The resultant list is :") print(my_result)OutputThe ... Read More
When it is required to extract rows of a matrix with even frequency elements, a list comprehension with ‘all’ operator and ‘Counter’ method are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all( value % 2 == 0 for key, value in list(dict(Counter(sub)).items()))] print("The result is :") print(my_result)OutputThe list is : [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], ... Read More
When it is required to convert a list into matrix with the size of every row increasing by a number, the ‘//’ operator and a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] print("The list is :") print(my_list) my_key = 3 print("The value of key is ") print(my_key) my_result = [] for index in range(0, len(my_list) // my_key): my_result.append(my_list[0: (index + 1) * my_key]) print("The resultant matrix is :") print(my_result)OutputThe list is : [42, 45, 67, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP