Found 10784 Articles for Python

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

586 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

564 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

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

190 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

Program to check congruency of two triangles in Python

Hafeezul Kareem
Updated on 06-Jul-2020 09:34:32

219 Views

In this tutorial, we are going to check the congruency of two triangles. We are going to check SSS, SAS, and AAA. The similarity of the triangles is proved based on those criteria.We have to check different conditions based on the theorem. Check them in the code below.Example Live Demodef side_side_side(sides_one, sides_two):    # sorting same pace    sides_one.sort()    sides_two.sort()    # checking the conditions    if sides_one[0] / sides_two[0] == sides_one[1] / sides_two[1] \       and sides_one[1] / sides_two[1] == sides_one[2] / sides_two[2] \       and sides_one[2] / sides_two[2] == sides_one[0] / sides_two[0]:     ... Read More

Program for longest common directory path in Python

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

367 Views

In this tutorial, we are going to write a program that finds the longest common path from the given list of paths. Let's see an example to understand the problem statement more clearly.Inputpaths = ['home/tutorialspoint/python', 'home/tutorialspoint/c', 'home/tutorialspoint/javascript', 'home/tutorialspoint/react', 'home/tutorialspoint/django']/home/tutorialspoint/We can solve the problem using os module very easily. Let's see the steps to solve theImport the os module.Initialize the list of paths to find the longest common path.Find the common prefix of all paths using os.path.commonprefix(paths) and store it in variable.And extract the directory from the common prefix using os.path.dirname(common_prefix).Example Live Demo# importing the os module import os # initializing the ... Read More

Binary Prefix Divisible By 5 in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:19:15

112 Views

Suppose we have an array A of 0s and 1s, consider N[i] is the i-th subarray from index A[0] to A[i] interpreted as a binary number. We have to find a list of boolean answers, where answer[i] is true if and only if N[i] is divisible by 5.So, if the input is like [0, 1, 1, 1, 1, 1], then the output will be [true, false, false, false, true, false]To solve this, we will follow these steps −length := size of Aans:= make an array of size length, and fill with falsenumber:= a binary value by concatenating each element from ... Read More

Advertisements