Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1958 of 3366
627 Views
In python data analysis, we may come across situation when we need to compare two lists and find out if they are identical meaning having same elements or not.Exmple Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu'] listB = ['Mon', 'Wed', 'Tue', 'Thu'] # Given lists print("Given listA: ", listA) print("Given listB: ", listB) # Sort the lists listA.sort() listB.sort() # Check for equality if listA == listB: print("Lists are identical") else: print("Lists are not identical")OutputRunning the above code gives us the following result −Given listA: ['Mon', 'Tue', 'Wed', 'Thu'] Given listB: ['Mon', 'Wed', 'Tue', 'Thu'] Lists are identicalWith ... Read More
278 Views
In python data analysis we may come across a scenario to check if a given substring is part of a bigger string. We will achieve this through the following programs.With findThe find function finds the first occurrence of the specified value. If the value is not found then it returns -1. We will apply this function to the given string and design a if clause to find out is substring is part of a string.Example Live DemoAstring = "In cloud 9" Asub_str = "cloud" # Given string and substring print("Given string: ", Astring) print("Given substring: ", Asub_str) if (Astring.find(Asub_str) == -1): ... Read More
6K+ Views
Python provides various methods to check if one list is a subset of another like 'all()' function and also by using 'issubset()' function to perform this check effectively. The three primary approaches used for checking if one list is a subset of another in Python are as follows. all() function : Checks if all elements in list of an iterable are true. issubet() method : Used in python sets for collecting of unique elements. intersection() method : ... Read More
1K+ Views
Given a list, we may need to check for the sequence of its elements. In this article we will find out if the elements present in the list are in a strictly increasing order. Below programs achieve that objective.With all and zipIn this approach we first slice each element compare its value to the next element that is sliced. If all such comparisons hold true then we conclude the list is strictly in increasing order.Example Live DemolistA = [11, 23, 42, 51, 67] #Given list print("Given list : ", listA) # Apply all and range if (all(i < j for i, ... Read More
6K+ Views
Lists are the most widely used data collectios in python. We may come across situation when we need to know if the given list is already sorted or not. In this article we will see the approaches to achieve this.With sortWe take a copy of the given list, apply sort function to it and store that copy as a new list. Then we compare it with the original list and check if they are equal or not.Example Live DemolistA = [11, 23, 42, 51, 67] #Given list print("Given list : ", listA) listA_copy = listA[:] # Apply sort to copy listA_copy.sort() ... Read More
3K+ Views
Depending on the needs of our data analysis we may need to check for presence of sequential numbers in a python data container. In the below programs we find out if among the elements of Alist, there are any consecutive numbers.With range and sortedThe sorted function will rearrange the elements of the list in a sorted order. We then apply the range function taking the lowest and highest numbers form the list using min and max functions. We store the results of above operations in two lists and compare them for equality.Example Live DemolistA = [23, 20, 22, 21, 24] sorted_list ... Read More
577 Views
We sometimes need to check if a required string can be formed from many number of strings that are present in a list. It also should not matter in what order the strings are present in the list which have to be joined to get the required string.With permutationsFrom itertools we can use the permutations function which will give us the possible combinations of the strings in the list in various order. As soon as a given combination matches the required string, we conclude that the string can be formed.Example Live Demofrom itertools import permutations chk_str = 'balloon' Alist = ... Read More
2K+ 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
1K+ 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
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