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 20 of 855
Assign multiple variables with a Python list values
Python allows you to assign multiple variables from a list in several ways. This is useful when you need to extract specific values from a list and use them as separate variables in your program. Using List Comprehension with Indexing You can use list comprehension to select specific elements by their index positions and assign them to variables ? schedule = ['Mon', ' 2pm', 1.5, '11 miles'] # Given list print("Given list:", schedule) # Using list comprehension with specific indices day, hours, distance = [schedule[i] for i in (0, 2, 3)] # Result ...
Read MoreAppend multiple lists at once in Python
For various data analysis work in Python, we may need to combine many Python lists into one list. This helps process it as a single input for other parts of the program. It provides performance gains by reducing the number of loops required for processing the data further. Using + Operator The + operator does a straightforward job of joining lists together. We apply the operator between the names of the lists and store the final result in a new list. The sequence of elements in the lists is preserved. Example listA = ['Mon', 'Tue', ...
Read MoreGet positive elements from given list of lists in Python
Lists can be nested, meaning the elements of a list are themselves lists. In this article we will see how to extract only the positive numbers from a list of lists. The result will be a new list containing nested lists with only positive numbers. Using List Comprehension List comprehension provides a concise way to filter positive elements from nested lists. We use nested list comprehension to iterate through each sublist and filter elements greater than zero ? Example listA = [[-9, -1, 3], [11, -8, -4, 434, 0]] # Given list print("Given List ...
Read MoreFinding frequency in list of tuples in Python
When working with lists containing tuples, you may need to find how frequently a specific element appears across all tuples. Python provides several efficient methods to count occurrences of elements within tuple structures. Using count() and map() The map() function extracts elements from each tuple, then count() finds the frequency of a specific element ? # initializing list of tuples fruits_days = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')] # Given list print("Given list of tuples:", fruits_days) # Frequency in list of tuples freq_result = list(map(lambda i: i[0], fruits_days)).count('Apple') # ...
Read MoreFind sum of frequency of given elements in the list in Python
When working with lists containing repeated elements, we often need to find the sum of frequencies for specific items. Python provides several approaches to calculate this efficiently ? Using sum() with count() This method uses the built-in count() method to find frequency of each element and sum() to calculate the total ? chk_list = ['Mon', 'Tue'] big_list = ['Mon', 'Tue', 'Wed', 'Mon', 'Mon', 'Tue'] # Apply sum res = sum(big_list.count(elem) for elem in chk_list) # Printing output print("Given list to be analysed:") print(big_list) print("Given list with values to be analysed:") print(chk_list) print("Sum of the ...
Read MoreFind frequency of given character at every position in list of lists in Python
Let's consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement. Consider a list of lists given below: listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']] print("Original list of lists:") print(listA) Original ...
Read MoreExtract only characters from given string in Python
Sometimes strings contain a mix of letters, numbers, and special characters. When you need to extract only the alphabetic characters from such strings, Python provides several efficient methods. Using isalpha() Method The isalpha() method checks if a character is alphabetic. You can combine it with a loop and join() to extract only letters ? Example text = "Qwer34^&t%y" # Given string print("Given string:", text) # Extract characters using isalpha() result = "" for char in text: if char.isalpha(): result = "".join([result, ...
Read MoreExtract numbers from list of strings in Python
While using Python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers from letters which can be used for future calculations. Using split() Method The split() function splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and numbers. We will use that along with list comprehension to extract each number ? days_with_numbers = ['Mon-2', ...
Read MoreEquate two list index elements in Python
During data manipulation with Python, we may need to bring two lists together and equate the elements in each of them pair wise. This means the element at index 0 from list 1 will be equated with element from index 0 of list 2 and so on. Using Tuple Formatting The tuple function can be leveraged to take elements from each list in sequence and match them up. We first store the result in a temp string which has the pattern in which the output of matching up of the values from lists will be displayed ? ...
Read MoreElement with largest frequency in list in Python
Finding the element with the largest frequency in a list is a common task in data analysis and statistics. Python provides several built-in approaches to accomplish this efficiently using the collections.Counter class and the statistics.mode function. Using Counter from collections The Counter class provides a most_common() method that returns elements with their frequencies in descending order. You can pass a parameter to limit the number of results ? Example from collections import Counter # Given list days_and_numbers = ['Mon', 'Tue', 'Mon', 9, 3, 3] print("Given list:", days_and_numbers) # Find the single most ...
Read More