Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1615 of 3366
8K+ Views
In this article, we are going to learn how to insert an item in a sorted list maintaining the order. Python has a built-in module called bisect that helps us to insert any element in an appropriate position in the list.Follow the below steps to write the code.Import the module bisect.Initialize list and element that need to insertThe module bisect has a method called insort that inserts an element into a list in an appropriate position. Use the method and insert the element.Print the list.Example Live Demo# importing the module import bisect # initializing the list, element numbers = [10, ... Read More
2K+ Views
In this article, we are going to learn how to intersect two dictionaries using keys. We have to create a new dictionary with common keys. Let's see an example.Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3}We are going to use the dictionary comprehension to solve the problem. Follow the below steps to write the code.Initialize dictionaries.Iterate over the dictionary one and add elements that are not in dictionary two.Print the result.Example Live Demo# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = ... Read More
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
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
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
327 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
442 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
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
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
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