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
Server Side Programming Articles
Page 19 of 2110
Check if list contains consecutive numbers in Python
Checking if a list contains consecutive numbers is a common task in data analysis. Python provides several approaches to verify if all elements in a list form a continuous sequence when arranged in order. Using range() and sorted() This method sorts the list and compares it with a range of consecutive numbers from minimum to maximum value ? Example numbers_a = [23, 20, 22, 21, 24] sorted_list = sorted(numbers_a) range_list = list(range(min(numbers_a), max(numbers_a) + 1)) if sorted_list == range_list: print("numbers_a has consecutive numbers") else: print("numbers_a has ...
Read MoreCheck if given string can be formed by concatenating string elements of list in Python
We sometimes need to check if a required string can be formed from multiple strings present in a list. The order of strings in the list doesn't matter — they can be concatenated in any sequence to form the target string. Using Permutations The itertools.permutations() function generates all possible combinations of strings in various orders. We check each combination by joining the strings until we find a match ? from itertools import permutations chk_str = 'balloon' string_list = ['fly', 'on', 'o', 'hot', 'ball', 'air'] def can_form_string(target, strings): for i in ...
Read MoreCheck if given multiple keys exist in a dictionary in Python
During data analysis using Python, we may need to verify if multiple keys exist in a dictionary. This allows us to ensure all required keys are present before proceeding with further operations. In this article, we will explore three different approaches to check if given multiple keys exist in a dictionary. Using Set Comparison Operators The most efficient approach uses set comparison operators. We convert the keys to check into a set and compare it with the dictionary's key set using the >= operator, which checks if all elements in the left set are present in the right ...
Read MoreCheck if element exists in 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 find out if a given element is present in the sublists which are themselves elements in the bigger list. Using any() with List Comprehension The any() function returns True if any element in an iterable is True. We can combine it with a generator expression to check if an element exists in any sublist ? nested_list = [[-9, -1, 3], [11, -8], [-4, 434, 0]] search_element = -8 # Given list print("Given List:", nested_list) ...
Read MoreAssign range of elements to List in Python
Lists are very frequently used data containers in Python. While using lists, we may come across a situation where we need to add a sequence of numbers to an existing list. We can add this sequence of numbers to a list using several Python functions. In this article, we will explore different ways of doing that. Using range() with extend() The extend() function allows us to increase the number of elements in a list. We use the range() function and apply extend() to the list so that all the required sequence of numbers are added at the end ...
Read MoreAssign 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 More