Hafeezul Kareem

Hafeezul Kareem

259 Articles Published

Articles by Hafeezul Kareem

Page 12 of 26

Python - List Initialization with alternate 0s and 1s

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 1K+ 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.# initialzing an empty list result = [] length = 7 # iterating for i in range(length):    # checking the index    if i % 2 == 0:       # appending 1 ...

Read More

List expansion by K in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 249 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.# 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 the ...

Read More

List consisting of all the alternate elements in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 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.# 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 result.[2, ...

Read More

Last occurrence of some element in a list in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 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.# initializing the list words = ['eat', 'sleep', 'drink', 'sleep', 'drink', 'sleep', 'go', 'come'] element = 'sleep' # reversing the list words.reverse() # ...

Read More

Python - Largest number possible from list of given numbers

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 2K+ 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.# importing the module import itertools ...

Read More

Python - Joining unicode list elements

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 471 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# 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, then ...

Read More

Python - Joining only adjacent words in list

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 362 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# 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 result ...

Read More

Python - Intersection of two String

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 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# 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 - Intersection of multiple lists

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 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# 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 - Intersect two dictionaries through keys

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 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# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': ...

Read More
Showing 111–120 of 259 articles
« Prev 1 10 11 12 13 14 26 Next »
Advertisements