Found 10476 Articles for Python

Python - Intersection of multiple lists

Hafeezul Kareem
Updated on 13-Nov-2020 19:08:20

1K+ Views

In this article, we are going to see how to intersect two lists that contain multiple lists in different ways. Let's start in the traditional way.Follow the below the steps to solve the problemInitialize two lists with multiple listsIterate over the first list and add the current item in the new list if it presents in the second list as well.Print the result.Example Live Demo# initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] ... Read More

Python - Intersection of two String

Hafeezul Kareem
Updated on 13-Nov-2020 19:02:25

4K+ Views

In this article, we are going to learn how to intersect two strings in different ways.Follow the below the steps to solve the problem.Initialize two strings and an empty string.Iterate over the first string and add the current character to the new string if it presents in the second string as well and not present in new string already.Print the result.Example Live Demo# initializing the string string_1 = 'tutorialspoint' string_2 = 'tut' result = '' # finding the common chars from both strings for char in string_1:    if char in string_2 and not char in result:     ... Read More

Python - Join tuple elements in a list

Hafeezul Kareem
Updated on 13-Nov-2020 18:56:17

11K+ Views

In this article, we are going to learn how to join tuple elements in a list. It's a straightforward thing using join and map methods. Follow the below steps to complete the task.Initialize list with tuples that contain strings.Write a function called join_tuple_string that takes a tuple as arguments and return a string.Join the tuples in the list using map(join_tuple_string, list) method.Convert the result to list.Print the result.Example# initializing the list with tuples string_tuples = [('A', 'B', 'C'), ('Tutorialspoint', 'is a', 'popular', 'site', 'for tech learnings')] # function that converts tuple to string def join_tuple_string(strings_tuple) -> str:    return ... Read More

Python - Joining only adjacent words in list

Hafeezul Kareem
Updated on 13-Nov-2020 18:52:49

314 Views

In this article, we are going to learn how to join adjacent words in a list, not digits. Follow the below steps to solve the problem.Initialize the list.Find the words that are not digits using isalpha method.4Join the words using join method.Add all the digits at the end by finding them using the isdigit method.Print the result.Example Live Demo# initialzing the list strings = ['Tutorials', '56', '45', 'point', '1', '4'] # result result = [] words = [element for element in strings if element.isalpha()] digits = [element for element in strings if element.isdigit()] # adding the elements to ... Read More

Python - Joining unicode list elements

Hafeezul Kareem
Updated on 13-Nov-2020 18:47:39

430 Views

In this article, we are going to learn how to join Unicode list elements. Follow the below steps to write the code.Initialize the list.Convert all the elements into Unicode using map and string.encode methods.Convert each encoded string using the decode method.Join the strings using join method.Print the result.Example Live Demo# initializing the list strings = ['Tutorialspoint', 'is a popular', 'site', 'for tech leranings'] def get_unicode(string): return string.encode() # converting to unicode strings_unicode = map(get_unicode, strings) # joining the unicodes result = ' '.join(unicode.decode() for unicode in strings_unicode) # printing the result print(result)If you run the above code, ... Read More

Python - Largest number possible from list of given numbers

Hafeezul Kareem
Updated on 13-Nov-2020 18:43:27

1K+ Views

In this article, we are going to learn how to find the possible largest number from the given list of numbers. We will see two different ways to find to solve the problem. Follow the below steps to solve the problem.Import the itertools module for permutations method.Initialize the list with numbers and an empty list.Iterate over the permutations of the list.Join all the combinations and add the result to the empty list.Find the max number from the result with max method and key as int.Convert the string to integer and print it.ExampleLet's see the code. Live Demo# importing the module import ... Read More

Last occurrence of some element in a list in Python

Hafeezul Kareem
Updated on 13-Nov-2020 18:34:57

11K+ Views

In this article, we are going to see different ways to find the last occurrence of an element in a list.Let's see how to find the last occurrence of an element by reversing the given list. Follow the below steps to write the code.Initialize the list.Reverse the list using reverse method.Find the index of the element using index method.The actual index of the element is len(list) - index - 1.Print the final index.ExampleLet's see the code. Live Demo# initializing the list words = ['eat', 'sleep', 'drink', 'sleep', 'drink', 'sleep', 'go', 'come'] element = 'sleep' # reversing the list words.reverse() ... Read More

ldexp() function in Python

Hafeezul Kareem
Updated on 13-Nov-2020 18:27:42

148 Views

In this article, we are going see how to use ldexp() function. This is one of the methods from the math library.The function ldexp(first, second) take two valid number either positive or negative and returns the result of first * (2 ** second). Let's see some examples.Example Live Demo# importing the math library import math # using the function ldexp print(math.ldexp(1, 4)) print(math.ldexp(5, -4)) print(math.ldexp(-3, -1))If you run the above code, then you will get the following result.Output16.0 0.3125 -1.5We will get error if we pass arguments other than numbers to the ldexp function. Let's see an example.Example Live Demo# importing ... Read More

Linear search on list or tuples in Python

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

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

Advertisements