Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 818 of 855
Convert list of string to list of list in Python
In this article we will see how to convert a list of strings that represent lists into actual nested lists. This is useful when you have string representations of lists and need to convert them back to proper list structures. Using strip() and split() The strip() method removes the square brackets, while split() separates the elements by comma and space ? Example string_list = ['[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]'] print("The given list is:") print(string_list) print() # using strip() + split() result = [item.strip("[]").split(", ") for item in string_list] print("Converting list of ...
Read MorePython - Clearing list as dictionary value
In Python, you may have a dictionary where values are lists and need to clear all the list values while keeping the keys. There are two main approaches: using the clear() method to empty existing lists in-place, or using dictionary comprehension to assign new empty lists. Using Loop with clear() Method The clear() method empties existing lists in-place, preserving the original list objects ? fruits = {"Apple": [4, 6, 9, 2], "Grape": [7, 8, 2, 1], "Orange": [3, 6, 2, 4]} print("Original dictionary:", fruits) # Clear each list in-place for key in fruits: ...
Read MorePython - Check if frequencies of all characters of a string are different
In this article, we will see how to find the frequency of each character in a given string and check if all characters have different frequencies. We'll accomplish this in two steps: first calculating character frequencies, then checking if all frequencies are unique. Finding Character Frequencies We can count character frequencies using a dictionary. For each character in the string, we either increment its count or initialize it to 1 ? Example in_string = "She sells sea shells" char_freq = {} for char in in_string: if char in char_freq.keys(): ...
Read MorePython - Check if dictionary is empty
During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In this article we will see how to check if a dictionary is empty or not. Using if Statement The if condition evaluates to True if the dictionary has elements. Otherwise it evaluates to False. This is the most Pythonic way to check dictionary emptiness ? Example dict1 = {1: "Mon", 2: "Tue", 3: "Wed"} dict2 = {} # Given dictionaries print("The original dictionary : ", (dict1)) print("The original dictionary : ", (dict2)) # Check ...
Read MorePossible Words using given characters in Python
In this article, we'll see how to find possible words that can be formed using a given set of characters. We'll take a list of reference words and a list of available characters, then determine which words can be made from those characters. The program uses two functions: one to count character frequencies, and another to check if each word can be formed from the available characters. Example Here's how to find words that can be formed from given characters: def count_characters(character): char_count = {} for n ...
Read MoreAccessing all elements at given Python list of indexes
Sometimes we need to access multiple elements from a list at specific index positions. Python provides several efficient approaches to extract elements at given indices from a list. Using List Comprehension The most Pythonic approach uses list comprehension to iterate through the index list and extract corresponding elements ? days = ["Mon", "Tue", "Wed", "Thu", "Fri"] indices = [1, 3, 4] # printing the lists print("Given list: " + str(days)) print("List of indices: " + str(indices)) # use list comprehension result = [days[i] for i in indices] # Get the result print("Result list: ...
Read MorePython - Convert column to separate elements in list of lists
When working with data structures in Python, you often need to reshape lists by separating columns into different elements. This is particularly useful when converting tabular data into different formats or extracting specific column ranges from nested lists. Using List Slicing and Comprehension You can slice lists at specific positions to create a columnar structure. This approach splits each sublist into two parts: elements from index 2 onwards and elements from index 0 to 2 ? Example data = [[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]] print("The given input ...
Read MorePython - Convert a set into dictionary
Python provides lot of flexibility to handle different types of data structures. There may be a need when you have to convert one Data Structure to another for a better use or better analysis of the data. In this article we will see how to convert a Python set to a Python dictionary. Using zip() and dict() The dict() function can be used to take input parameters and convert them to a dictionary. We also use the zip() function to group the keys and values together which finally become the key-value pairs in the dictionary ? Example ...
Read MoreHow to print double quotes with the string variable in Python?
Printing double quotes with string variables can be tricky since double quotes are part of Python's string syntax. This article explores several methods to include double quotes in your printed output. Common Mistakes The following examples show what not to do when trying to print double quotes ? print(" ") print(" " " ") print(""aString"") The output of the above code is ? File "", line 3 print(""aString"") ^ SyntaxError: invalid syntax Method 1: ...
Read MoreBigram formation from given a Python list
A bigram is a pair of consecutive words formed from a sentence. In Python, bigrams are heavily used in text analytics and natural language processing to analyze word patterns and relationships. What is a Bigram? A bigram takes every two consecutive words from a sentence and creates word pairs. For example, from "hello world python", we get bigrams: ("hello", "world") and ("world", "python"). Using enumerate() and split() This approach splits the sentence into words and uses enumerate() to create pairs from consecutive words ? sentences = ['Stop. look left right. go'] print("The given list ...
Read More