In this article, we are going to learn how to initialize a list with alternate 0s and 1s. We'll have list length and need to initialize with alternate 0s and 1s.Follow the below steps to initialize a list with alternate 0s and 1s.Initialize an empty list and length.Iterate length times and append 0s and 1s alternatively based on the index.Print the result.ExampleLet's see the code. Live Demo# initialzing an empty list result = [] length = 7 # iterating for i in range(length): # checking the index if i % 2 == 0: # appending ... Read More
In this article, we are going to learn how to convert the list of tuples into a dictionary. Converting a list of tuples into a dictionary is a straightforward thing.Follow the below steps to complete the code.Initialize the list with tuples.Use the dict to convert given list of tuples into a dictionary.Print the resultant dictionary.ExampleLet's see the code.# initializing the list tuples = [('Key 1', 1), ('Key 2', 2), ('Key 3', 3), ('Key 4', 4), ('Key 5', 5)] # converting to dict result = dict(tuples) # printing the result print(result)If you run the above code, you will get ... Read More
In this article, we are going to learn how to make pairs from two lists such that no similar elements make a pair. Follow the below steps to solve the problem.Initialize the lists with elements.Iterate over the lists and append the pair into a list if the corresponding elements from the lists are not same.Print the result.ExampleLet's see the code.# initializing the lists list_1 = [1, 2, 3, 4, 5] list_2 = [5, 8, 7, 1, 3, 6] # making pairs result = [(i, j) for i in list_1 for j in list_2 if i != j] # ... Read More
In this article, we are going to learn how to use SQL with Python and SQLite database. Python has a built-in module to connect with SQLite database. We are going to use sqlite3 module to connect Python and SQLite.We have to follow the below steps to connect the SQLite database with Python. Have a look at the steps and write the program.Import the sqlite3 module.Create a connection using the sqlite3.connect(db_name) the method that takes a database name is an argument. It creates one file if doesn't exist with the given name else it opens the file with the given name.Get ... Read More
In this article, we are going to learn about the pytrie module to prefix matching strings from a list of strings. Let's see an example to understand it clearly.Input: List: ['tutorialspoint', 'tutorials', 'tutorialspython', 'python'] Prefix: 'tutorials' Output: ['tutorialspoint', 'tutorials', 'tutorialspython']We can achieve it in different ways. In this tutorial, we are going to achieve it using the pytrie module.From pytrie module, we will use the pytrie.StringTrie data structure. We can perform create, insert, search, and delete operations.First, install the pytrie module with the following command.pip install pytrieLet's see steps to achieve the desired output.Import the pytrie module.Initialize the list, ... Read More
In this article, we are going to learn how to validate a form in django. Django comes with build-in validators for forms. We can use them in the tutorial to validate forms.You must familiar with the Django to follow along with this tutorial. If you are not familiar with Django, then this article is not for you.Set up the basic Django project with the following commands.mkdir form_validation cd form_validation python -m venv env (Activate environment based on your OS) pip install django===3.0 django-admin startproject form_validation . (Don't forget dot(.) at the end) python manage.py startapp ... Read More
In this article, we are going to scrape the text from Wikipedia's Infobox using BeatifulSoup and requests in Python. We can do it in 10 mins. It's straightforward.We need to install bs4 and requests. Execute the below commands to install.pip install bs4 pip install requestsFollow the below steps to write the code to fetch the text that we want from the infobox.Import the bs4 and requests modules.Send an HTTP request to the page that you want to fetch data from using the requests.get() method.Parse the response text using bs4.BeautifulSoup class and store it in a variable.Go to the Wikipedia page ... Read More
IntroductionCURL context options are available when the CURL extension was compiled using the --with-curlwrappers configure option. Given below is list of CURL wrapper context optionsMethodDescriptionmethodHTTP method supported by the remote server. Defaults to GET.headerAdditional headers to be sent during requestuser_agentValue to send with User-Agent: header.contentAdditional data to be sent after the headers. This option is not used for GET or HEAD requests.proxyURI specifying address of proxy server.max_redirectsThe max number of redirects to follow. Defaults to 20.curl_verify_ssl_hostVerify the host. Defaults to FALSE. available for both http and ftp protocol wrappers.curl_verify_ssl_peerRequire verification of SSL certificate used. Defaults to FALSE. available for both ... Read More
Suppose we are considering an infinite number line, here the position of the i−th stone is given by array stones and stones[i] is indicating ith stone position. A stone is an endpoint stone if it has the smallest or largest position. Now in each turn, we pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.If the stones are at say, stones = [1, 2, 5], we cannot move the endpoint stone at position 5, because moving it to any position (such as 0, or 3) will still keep ... Read More
Suppose we have an array A of integers; we have to find the maximum sum of elements in two non−overlapping subarrays. The lengths of these subarrays are L and M.So more precisely we can say that, we have to find the largest V for whichV = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either −0 = 0, decrease i by 1, decrease j by 1, do −rightL[i + 1] := max of temp and x where x is 0 if i + 2 >= n otherwise x = rightL[i + 2]temp ... Read More