Found 10476 Articles for Python

List expansion by K in Python

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

203 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

List frequency of elements in Python

Hafeezul Kareem
Updated on 26-Aug-2023 03:26:58

56K+ Views

In this article, we are going to learn how to find the frequency of elements in a Python list. We can solve the problem in different ways. Let's see two of them.Follow the below steps to write the code.Initialize the list with elements and an empty dictionary.Iterate over the list of elements.Check whether the element is present in the dictionary or not.If the element is already present in the dictionary, then increase its count.If the element is not present in the dictionary, then initialize its count with 1.Print the dictionary.ExampleLet's see the code. Live Demo# initializing the list random_list = ['A', ... Read More

Python - List Initialization with alternate 0s and 1s

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

997 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

Python - Make pair from two list such that elements are not same in pairs

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

1K+ 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

448 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

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

How to select multiple DataFrame columns using regexp and datatypes

Kiran P
Updated on 10-Nov-2020 10:04:29

797 Views

DataFrame maybe compared to a data set held in a spreadsheet or a database with rows and columns. DataFrame is a 2D Object.Ok, confused with 1D and 2D terminology ?The major difference between 1D (Series) and 2D (DataFrame) is the number of points of information you need to inorer to arrive at any single data point. If you take an example of a Series, and wanted to extract a value, you only need one point of reference i.e. row index.In comparsion to a table (DataFrame), one point of reference is not sufficient to get to a data point, you need ... Read More

Program to find airports in correct order in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:37:21

370 Views

Suppose we have a list of flights as [origin, destination] pairs. The list is shuffled; we have to find all the airports that were visited in the correct order. If there are more than one valid itinerary, return lexicographically smallest ones first.So, if the input is like flights = [["Mumbai", "Kolkata"], ["Delhi", "Mumbai"], ["Kolkata", "Delhi"] ], then the output will be ['Delhi', 'Mumbai', 'Kolkata', 'Delhi']To solve this, we will follow these stepsins := an empty mapouts := an empty mapadj_list := an empty mapDefine a function dfs() . This will take airportwhile outs[airport] is not null, donxt := size of ... Read More

Advertisements