Found 10476 Articles for Python

Python How to copy a nested list

Hafeezul Kareem
Updated on 07-Jul-2020 08:53:49

480 Views

In this tutorial, we are going to see different ways to copy a nested list in Python. Let's see one by one.First, we will copy the nested list using loops. And it's the most common way.Example Live Demo# initializing a list nested_list = [[1, 2], [3, 4], [5, 6, 7]] # empty list copy = [] for sub_list in nested_list:    # temporary list    temp = []    # iterating over the sub_list    for element in sub_list:       # appending the element to temp list       temp.append(element)    # appending the temp list to copy ... Read More

Python Indices of numbers greater than K

Hafeezul Kareem
Updated on 07-Jul-2020 08:52:46

560 Views

In this tutorial, we are going to find the indices of the numbers that are greater than the given number K. Let's see the different ways to find them.A most common way to solve the problem is using the loops. Let's see the steps to solve the problem.Initialize the list and K.Iterate over the list using its length.If you find any number greater than K, then print the current index.Example Live Demo# initializing the list and K numbers = [3, 4, 5, 23, 12, 10, 16] K = 10 # iterating over thAe list for i in range(len(numbers)):    # checking ... Read More

Python Indexing a sublist

Hafeezul Kareem
Updated on 07-Jul-2020 08:51:13

2K+ Views

In this tutorial, we are going to write a program that finds the index of a sublist element from the list. Let's see an example to understand it clearly.Inputnested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]OutputIndex of 7:- 2 Index of 5:- 1 Index of 3:- 0Let's see the simple and most common way to solve the given problem. Follow the given steps solve it.Initialize the list.Iterate over the list using the index.Iterate over the sub list and check the element that you want to find the index.If we find the element then print and break itExample Live ... Read More

Python Index specific cyclic iteration in list

Hafeezul Kareem
Updated on 07-Jul-2020 08:49:32

1K+ Views

In this tutorial, we are going to write a program that cyclically iterates a list from the given Let's see the steps to solve the problemInitialize the list and index.Find the length of the list using len.Iterate over the list using the length.Find the index of the element using index % length.Print the element.Increment the index.It's a simple loop iteration. You can write it without any trouble. Let's see the code.Example Live Demo# initializing the list and index alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] start_index = 5 # finding the length length = len(alphabets) # iterating over ... Read More

Python How to sort a list of strings

Hafeezul Kareem
Updated on 07-Jul-2020 08:44:24

747 Views

In this tutorial, we are going to see how to sort a list of strings. We'll sort the given list of strings with sort method and sorted function. And then we'll see how to sort the list of strings based on different criteria like length, value, etc., Let's see how to sort a list of strings using list.sort() method. The sort method list is an insort. It'll directly sort the original list. Let's see the code.Example Live Demo# list of strings strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring'] # sorting the list in ascending order strings.sort() # printing the ... Read More

Python Holidays library

Hafeezul Kareem
Updated on 07-Jul-2020 08:41:43

2K+ Views

In this tutorial, we are going to learn about the holidays library. The holidays library will help to determine whether a particular is a holiday or not in different countries. We can only see the public holidays.Let's install the module with the following command.pip install holidaysAfter successfully installing the module follow the ways to see get the holidays information. see the holidays of India in the year 2020. You have to follow the below steps to get a complete list of holidays for India in 2020.You can find the country codes for the holidays here..Import the holidays' module.Instantiate the holidays.India() ... Read More

Python Grouping similar substrings in list

Hafeezul Kareem
Updated on 07-Jul-2020 08:40:00

3K+ Views

In this tutorial, we are going to write a program that groups similar substrings from a list. Let's see an example to understand it more clearly.Inputstrings = ['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript', 'python-1', 'python-2', 'javascript-1']Output[['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript'], ['python-1', 'python-2'], ['javascript-1']]We are going to use the groupby method from itertools module to solve the problem. The groupby method will group all the similar string into an iter object. For the given list we have split the string with - and pass the first part of the string to the groupby method.Let's see the steps involved in solving this problem.Initialize the list ... Read More

Python Group elements at same indices in a multi-list

Hafeezul Kareem
Updated on 06-Jul-2020 09:40:01

684 Views

In this tutorial, we are going to write a program that combines elements of the same indices different lists into a single list. And there is one constraint here. All the lists must be of the same length. Let's see an example to understand it more clearly.Input[[1, 2, 3], [4, 5, 6], [7, 8, 9]]Output[[1, 4, 7], [2, 5, 8], [3, 6, 9]]We can solve it in different ways. Let's see how to solve with normal loops.Initialize the list with lists.Initialize an empty list.Initialize a variable index to 0.Iterate over the sub list length timesAppend an empty list to the ... Read More

Python Group Anagrams from given list

Hafeezul Kareem
Updated on 06-Jul-2020 09:37:28

4K+ Views

In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams.Any two strings that have the same character in a different order are known as anagrams.Before diving into the solution, let's see an example.Input['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']Output[['cat', 'tac'], ['dog', 'god'], ['fried', 'fired'], ['pat', 'tap']]We will breakdown the problem into two pieces. First, we will write a function that checks two strings are anagrams or not. Follow the below steps to write code to check anagrams.Initialize the strings.Sort both the strings.If both sorted strings are ... Read More

Python Getting sublist element till N

Hafeezul Kareem
Updated on 06-Jul-2020 09:35:59

242 Views

In this tutorial, we are going to write a program that returns a sublist element till nth sublist in a list. Let's say we have the following list with 5 sublists.[['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] Now, we have to get the first element from the first three sublists. We can get the elements different approaches. Let's see some of them.LoopsMore generic and first thought of the most of programmers is to use loops. Let's see the code using loops.Example Live Demo# initializing the list and N random_list = [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C# C++'], ... Read More

Advertisements