Found 10476 Articles for Python

Python - Contiguous Boolean Range

Pradeep Elance
Updated on 10-Jul-2020 11:36:51

337 Views

Given a list of values, we are interested to know at which position are the Boolean values present as a contiguous list. Which means after we encounter a value which is TRUE there is a continuous value of true from that position until FALSE value is found. Similarly when a FALSE is found there is a contiguous value of FALSE until TRUE is found.With itertoolsW can use accumulate along with groupby from the itertools module. In this example we take a given list and then apply the accumulate function to keep track of the values that are brought together using ... Read More

Python - Column deletion from list of lists

Pradeep Elance
Updated on 10-Jul-2020 11:34:40

1K+ Views

In a list of lists an element at the same index of each of the sublist represents a column like structure. In this article we will see how we can delete a column from a list of lists. Which means we have to delete the element at the same index position from each of the sublist.Using popWe use the pop method which removes the element at a particular position. A for loop is designed to iterate through elements at specific index and removes them using pop.Example Live Demo# List of lists listA = [[3, 9, 5, 1], [4, 6, 1, 2], ... Read More

Python - Check if given words appear together in a list of sentence

Pradeep Elance
Updated on 10-Jul-2020 11:32:49

1K+ Views

Say we have a list containing small sentences as its elements. We have another list which contains some of the words used in this sentences of the first list. We want to find out if two words from the second list are present together in some of the sentences of the first list or not.With append and for loopWe use the for loop with in condition to check for the presence of words in the lists of sentences. Then we use the len function to check if we have reached the end of the list.Example Live Demolist_sen = ['Eggs on Sunday', ... Read More

Python - Check if all elements in a list are identical

Pradeep Elance
Updated on 10-Jul-2020 11:30:02

928 Views

There may be occasions when a list will contain all the values which are same. In this article we will see various way to verify that.With allWe use the all function to find the result of comparison of each element of the list with the first element. If each comparison gives a result of equality then the result is given as all elements are equal else all elements are not equal.Example Live DemolistA = ['Sun', 'Sun', 'Mon'] resA = all(x == listA[0] for x in listA) if resA:    print("in ListA all elements are same") else:    print("In listA ... Read More

Python - Check if a list is contained in another list

Pradeep Elance
Updated on 10-Jul-2020 11:28:07

1K+ Views

Given two different python lists we need to find if the first list is a part of the second list.With map and joinWe can first apply the map function to get the elements of the list and then apply the join function to cerate a comma separated list of values. Next we use the in operator to find out if the first list is part of the second list.Example Live DemolistA = ['x', 'y', 't'] listB = ['t', 'z', 'a', 'x', 'y', 't'] print("Given listA elemnts: ") print(', '.join(map(str, listA))) print("Given listB elemnts:") print(', '.join(map(str, listB))) res = ', '.join(map(str, ... Read More

Python - Check if a given string is binary string or not

Pradeep Elance
Updated on 10-Jul-2020 11:25:39

7K+ Views

In this article we check if a given string has characters which are only 1 or 0. We call such strings as binary strings. If it has any other digit like 2 or 3 etc., we classify it as a non-binary string.With setThe set operator in python stores only unique elements. So we take a string and apply the set function to it. Then we create another set which has only 0 and 1 as its elements. If both these sets are equal then the string is definitely binary. Also the string may have only 1s or only 0s. So ... Read More

Password validation in Python

Pradeep Elance
Updated on 10-Jul-2020 11:22:16

3K+ Views

It is a general requirement to have a reasonably complex password. In this article we will see how to validate if a given password meats certain level of complexity. For that will use the regular expression module known as re.Example -1First we create a regular expression which can satisfy the conditions required to call it a valid password. Then we e match the given password with the required condition using the search function of re. In the below example the complexity requirement is we need at least one capital letter, one number and one special character. We also need the ... Read More

Multi-dimensional lists in Python

Pradeep Elance
Updated on 10-Jul-2020 11:19:39

9K+ Views

Lists are a very widely use data structure in python. They contain a list of elements separated by comma. But sometimes lists can also contain lists within them. These are called nested lists or multidimensional lists. In this article we will see how to create and access elements in a multidimensional list.Creating Multi-dimensional listIn the below program we create a multidimensional list of 4 columns and 3 rows using nested for loops.Example Live Demomultlist = [[0 for columns in range(4)] for rows in range(3)] print(multlist)OutputRunning the above code gives us the following result −[[0, 0, 0, 0], [0, 0, 0, 0], ... Read More

MongoDB and Python

Pradeep Elance
Updated on 10-Jul-2020 11:17:08

685 Views

MongoDB is a widely used document database which is also a form of NoSQL DB. Python can interact with MongoDB through some python modules and create and manipulate data inside Mongo DB. In this article we will learn to do that. But MongoDB should already be available in your system before python can connect to it and run. To setup MongoDB in your system please visit our  MongoDB tutorial here..Install pymongoTo interact with MongoDB we need the module names pymongo. Install it in your python environment using the below command.pip install pymogoCheck the Existing DBsWe now use this python module ... Read More

Launching parallel tasks in Python

Pradeep Elance
Updated on 10-Jul-2020 11:15:01

380 Views

If a Python program can be broken into subprograms who is processing do not depend on each other, then each of the subprogram can be run in parallel when the overall program is being run. This concept is known as parallel processing in Python.With multiprocessingThis module can be used to create many child processes of a main process which can run in parallel. In the below program we initialize a process and then use the run method to run the multiple sub-processes. We can see different sub processes in the print statement by using the process id. We also use ... Read More

Advertisements