Found 10784 Articles for Python

Check if given multiple keys exist in a dictionary in Python

Pradeep Elance
Updated on 13-May-2020 14:27:28

1K+ Views

During data analysis using python, we may need to verify if a couple of values exist as keys in a dictionary. So that the next part of the analysis can only be used with the keys that are part of the given values. In this article we will see how this can be achieved.With Comparison operatorsThe values to be checked are put into a set. Then the content of the set is compared with the set of keys of the dictionary. The >= symbol indicates all the keys from the dictionary are present in the given set of values.Example Live DemoAdict ... Read More

Check if element is present in tuple of tuples in Python

Pradeep Elance
Updated on 13-May-2020 14:24:13

978 Views

Python Tuples can be nested. We can have a tuple whose elements are also tuples. In this article we will see how to find out if a given value is present as an element in a tuple of tuples.With anyThe any function can be used to check if a given value is present as an element in any of the subtuples that are present in the tuple with help of for loop. We put the entire condition for checking in an if and else clause.Example Live DemoAtuple = [('Mon', 10), ('Tue', 8), ('Wed', 8), ('Thu', 5)] #Given tuple print("Given tuple: ... Read More

Check if element exists in list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:19:28

2K+ Views

Lists can be nested, means 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 sublist which are themselves elements in the bigger list.With anyWe first search if an element is present in the sublist and if the sublist is present in the list. If anyof this is true we can say that the element is present in the list.Example Live DemolistA = [[-9, -1, 3], [11, -8], [-4, 434, 0]] search_element = -8 # Given list print("Given List :", listA) print("Element to ... Read More

Check if a list exists in given list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:14:28

554 Views

Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out if a given list is present as an element in the outer bigger list.With inThis is a very simple and straight forward method. We use the in clause just to check if the inner list is present as an element in the bigger list.Example Live DemolistA = [[-9, -1, 3], [11, -8], [-4, 434, 0]] search_list = [-4, 434, 0] # Given list print("Given List :", listA) print("list to Search: ", search_list) # Using in if ... Read More

Assign range of elements to List in Python

Pradeep Elance
Updated on 13-May-2020 14:10:36

391 Views

Lists are very frequently used data container in Python. While using lists we may come across a situation where the elements of the list maybe a sequence of numbers. We can add this sequence of numbers to a list using many Python functions. In this article we will explore different ways of doing that.With range and extendThe extent function allows us to increase the number of elements in a list. Will use the range function and apply extend to the list so that all the required sequence of numbers are added at the end of the list.Example Live DemolistA = [55, ... Read More

Assign multiple variables with a Python list values

Pradeep Elance
Updated on 13-May-2020 14:08:51

2K+ Views

Depending on the need of the program we may an requirement of assigning the values in a list to many variables at once. So that they can be further used for calculations in the rest of the part of the program. In this article we will explore various approaches to achieve this.Using for inThe for loop can help us iterate through the elements of the given list while assigning them to the variables declared in a given sequence.We have to mention the index position of values which will get assigned to the variables.Example Live DemolistA = ['Mon', ' 2pm', 1.5, '11 ... Read More

Append multiple lists at once in Python

Pradeep Elance
Updated on 13-May-2020 14:06:09

8K+ Views

For various data analysis work in python we may be needed to combine many python lists into one list. This will help processing it as a single input list for the other parts of the program that need it. It provides performance gains by reducing number of loops required for processing the data further.Using + operatorThe + operator does a straight forward job of joining the lists together. We just apply the operator between the name of the lists and the final result is stored in the bigger list. The sequence of the elements in the lists are preserved.Example Live DemolistA ... Read More

Accessing nth element from Python tuples in list

Pradeep Elance
Updated on 13-May-2020 14:03:45

754 Views

A python list can contain tuples as its elements. In this article we will explore how to access every nth element form the tuples that are present as the elements in the given tuple.Using indexWe can design a for loop to access the elements from the list with the in clause applied for nth index. Then we store the result into a new list.Example Live DemoAlist = [('Mon', '3 pm', 10), ('Tue', '12pm', 8), ('Wed', '9 am', 8), ('Thu', '6 am', 5)] #Given list print("Given list: ", Alist) # Use index res = [x[1] for x in Alist] ... Read More

Get positive elements from given list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:00:46

356 Views

Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out only the positive numbers from a list of lists. In the result a new list will contain nested lists containing positive numbers.With for inHere we simply apply the mathematical operator to check for the value of elements in a list using a for loop. If the value is positive we capture it as a list and Outer for loop stores as a final list of lists.Example Live DemolistA = [[-9, -1, 3], [11, -8, -4, 434, 0]] ... Read More

Finding frequency in list of tuples in Python

Pradeep Elance
Updated on 05-May-2020 10:22:42

383 Views

Many different types of data container can get mixed up in python. A list can have elements each of which is a tuple. In this article we will take such a list and find the frequency of element in the tuples which are themselves elements of a list.Using count and mapWe apply a lambda function to count through each of the first element in the tuples present in the list. Then apply a map function to arrive at the total count of the element we are searching for.Example Live Demo# initializing list of tuples listA = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', ... Read More

Advertisements