Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program that print elements common at specified index of list elements

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 525 Views

When it is required to print the elements common at a specific index in a list of strings, a ‘min’ method, list comprehension and a Boolean flag value can be used.ExampleBelow is a demonstration of the samemy_list = ["week", "seek", "beek", "reek", 'meek', 'peek'] print("The list is :") print(my_list) min_length = min(len(element) for element in my_list) my_result = [] for index in range(0, min_length): flag = True for element in my_list: if element[index] != my_list[0][index]: ...

Read More

Python Program to print element with maximum vowels from a List

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 706 Views

When it is required to print element with maximum vowels from a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["this", "week", "is", "going", "great"] print("The list is :") print(my_list) my_result = "" max_length = 0 for element in my_list: vowel_length = len([element for element in element if element in ['a', 'e', 'o', 'u', 'i']]) if vowel_length > max_length: max_length = vowel_length my_result = element print("The result is :") print(my_result)OutputThe list is ...

Read More

Python - Round number of places after the decimal for column values in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 563 Views

To round number of places after the decimal, use the display.precision attribute of the Pandas.At first, import the required Pandas library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } ) Use the set_option() method to set precision. We have set precision value 2 −pd.set_option('display.precision', 2)ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( ...

Read More

Python program to find the String in a List

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 557 Views

When it is required to find the string in a list, a simple ‘if’ condition along with ‘in’ operator can be used.ExampleBelow is a demonstration of the samemy_list = [4, 3.0, 'python', 'is', 'fun'] print("The list is :") print(my_list) key = 'fun' print("The key is :") print(key) print("The result is :") if key in my_list: print("The key is present in the list") else: print("The key is not present in the list")OutputThe list is : [4, 3.0, 'python', 'is', 'fun'] The key is : fun The result is : The key is present ...

Read More

Python program for most frequent word in Strings List

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 5K+ Views

When it is required to find the most frequent word in a list of strings, the list is iterated over and the ‘max’ method is used to get the count of the highest string.ExampleBelow is a demonstration of the samefrom collections import defaultdict my_list = ["python is best for coders", "python is fun", "python is easy to learn"] print("The list is :") print(my_list) my_temp = defaultdict(int) for sub in my_list: for word in sub.split(): my_temp[word] += 1 result = max(my_temp, key=my_temp.get) print("The word that has the ...

Read More

Python program to get maximum of each key Dictionary List

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 286 Views

When it is required to get the maximum of each key in a list of dictionary elements, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Hi": 18, "there": 13, "Will": 89}, {"Hi": 53, "there": 190, "Will": 87}] print("The list is : ") print(my_list) my_result = {} for elem in my_list: for key, val in elem.items(): if key in my_result: my_result[key] = max(my_result[key], val) else: ...

Read More

Python program to count the pairs of reverse strings

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 216 Views

When it is required to count the pairs of reverse strings, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Python": 8, "is": 1, "fun": 9}, {"Python": 2, "is": 9, "fun": 1}, {"Python": 5, "is": 10, "fun": 7}] print("The list is :") print(my_list) result = {} for dic in my_list: for key, value in dic.items(): if key in result: result[key] = max(result[key], value) else: ...

Read More

Python Pandas - Convert Nested Dictionary to Multiindex Dataframe

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 2K+ Views

At first, let us create a Nested Dictionary −dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], 'Country': ['England', 'Canada', 'Germany'] }}Now, create an Empty Dictionary −new_dict = {}Now, loop to assign values −for outerKey, innerDict in dictNested.items(): for innerKey, values in innerDict.items(): new_dict[(outerKey, innerKey)] = values Convert to Multi-index DataFrame −pd.DataFrame(new_dict)ExampleFollowing is the code −import pandas as pd # Create Nested dictionary dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], ...

Read More

Python program to print elements which are multiples of elements given in a list

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 369 Views

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

Python Program to print strings based on the list of prefix

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 455 Views

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
Showing 28341–28350 of 61,248 articles
Advertisements