Linear Search on List or Tuples in Python

Hafeezul Kareem
Updated on 13-Nov-2020 18:21:40

4K+ Views

In this article, we are going to learn how to apply a linear search on lists and tuples.A linear search starts searching from the first element and goes till the end of the list or tuple. It stops checking whenever it finds the required element.Linear Search - Lists & TuplesFollow the below steps to implement linear search on lists and tuples.Initialize the list or tuple and an element.Iterate over the list or tuple and check for the element.Break the loop whenever you find the element and mark a flag.Print element not found message based on the flag.ExampleLet's see the code.# ... Read More

List Consisting of All Alternate Elements in Python

Hafeezul Kareem
Updated on 13-Nov-2020 18:16:46

3K+ Views

In this article, we are going to learn how to get alternate elements from the list. We'll see two different ways to solve the problem.Follow the below steps to solve the problem in one way.Initialize the list.3Iterate over the list and store all the elements from the odd index.Print the result.ExampleLet's see the code. Live Demo# Initializing the list numbers = [1, 2, 3, 4, 5] # finding alternate elements result = [numbers[i] for i in range(len(numbers)) if i % 2 != 0] # printing the result print(result)If you run the above code, then you will get the following ... Read More

List Expansion by K in Python

Hafeezul Kareem
Updated on 13-Nov-2020 18:08:03

213 Views

In this article, we are going to learn how to expand the list by replicating the element K times. We'll two different ways to solve the problem.Follow the below steps to solve the problem.Initialize the list, K, and an empty list.3Iterate over the list and add current element K times using replication operator.Print the result.ExampleLet's see the code. Live Demo# initializing the list numbers = [1, 2, 3] K = 5 # empty list result = [] # expanding the list for i in numbers:    result += [i] * K # printing the list print(result)If you run ... Read More

Python List Initialization with Alternate 0s and 1s

Hafeezul Kareem
Updated on 13-Nov-2020 17:47:10

1K+ Views

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

List of Tuples to Dictionary Conversion in Python

Hafeezul Kareem
Updated on 13-Nov-2020 17:36:46

11K+ Views

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

Make Pairs from Two Lists in Python Without Same Elements

Hafeezul Kareem
Updated on 13-Nov-2020 17:27:24

5K+ Views

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

SQL Using Python and SQLite

Hafeezul Kareem
Updated on 13-Nov-2020 13:09:38

2K+ Views

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

Prefix Matching in Python Using PyTrie Module

Hafeezul Kareem
Updated on 13-Nov-2020 13:03:21

461 Views

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

Form Validation Using Django

Hafeezul Kareem
Updated on 13-Nov-2020 12:58:00

4K+ Views

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

Fetching Text from Wikipedia's Infobox in Python

Hafeezul Kareem
Updated on 13-Nov-2020 12:41:01

1K+ Views

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

Advertisements