Python Articles

Page 134 of 852

Python - Ways to flatten a 2D list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

A list is a collection which is ordered and changeable. In Python lists are written with square brackets. You access the list items by referring to the index number. Negative indexing means beginning from the end,  -1 refers to the last item. You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.Example# using chain.from_iterables # import chain from itertools import chain ini_list = [[1, 2, 3],    [3, 6, 7],    [7, 5, 4]]     # ...

Read More

Python - Ways to invert mapping of dictionary

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 410 Views

Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning.Example# using dict comprehension # initialising dictionary ini_dict = {101: "vishesh", 201 : "laptop"} # print initial dictionary print("initial dictionary : ", str(ini_dict)) # inverse mapping using dict comprehension inv_dict = {v: k for k, v in ini_dict.items()} # print final dictionary print("inverse mapped dictionary : ", str(inv_dict)) # using zip and dict functions # initialising dictionary ini_dict = {101: "vishesh", 201 : ...

Read More

Python - Which is faster to initialize lists?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 242 Views

Python is a very flexible language where a single task can be performed in a number of ways, for example initializing lists can be performed in many ways. However, there are subtle differences in these seemingly similar methods. Python which is popular for its simplicity and readability is equally infamous for being slow compared to C++ or Java. The ‘for’ loop is especially known to be slow whereas methods like map() and filter() known to be faster because they are written in C.Example# import time module to calculate times import time # initialise lists to save the times forLoopTime = ...

Read More

Custom list split in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 567 Views

Data analytics throws complex scenarios where the data need to be wrangled to moved around. In this context let’s see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this.With zip and for loopIn this approach we use list dicing to get the elements from the point at which the splitting has to happen. Then we use zip and for loop to create the sublists using a for loop.ExampleAlist = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] # ...

Read More

Delete elements in range in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 876 Views

Deleting a single element from a python is straight forward by using the index of the element and the del function. But there may be scenarios when we need to delete elements for a group of indices. This article explores the approaches to delete only those elements form the list which are specified in the index list.Using sort and delIn this approach we create a list containing the index values where deletion has to happen. The we sort and reverse them to preserve the original order of the elements of the list. Finally we apply the del function to the ...

Read More

Delete items from dictionary while iterating in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 568 Views

A python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referred using the key. In this article we will explore the ways to delete the items form a dictionary.Using del with keysIn this approach we capture the key values that are needed to be deleted. Once we apply the del function, the key value pairs for those keys get deleted.Example# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4:'Thu', 5:'Fri'} # Get keys with value in 2, 3. to_del = [key for key in ADict if ...

Read More

Dictionary with index as value in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 892 Views

In this article we will learn how to create a dictionary from another frequently used python collection namely list. An index or key is not part a list content. But in dictionary we need to have a key or index attached to every element which is referred as value.Using enumerateThe enumerate function adds a counter as the key of the enumerate object. SO we apply it to a given list and using a for loop. That creates the required dictionary where the keys are generated by the enumerate function.ExampleAlist = ['Mon', 'Tue', 'Wed', 'Wed', 11, 11] # Given list ...

Read More

Dictionary creation using list contents in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 233 Views

Changing the collection type from one type to another is a very frequent need in python. In this article we will see how we create a dictionary when multiple lists are given. The challenge is to be able to combine all these lists to create one dictionary accommodating all these values in a dictionary key value format.With zipThe zip function can be used to combine the values of different lists as shown below. In the below example we have taken three lists as input and combine them to form a single dictionary. One of the list supplies the keys for ...

Read More

Difference of two lists including duplicates in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 334 Views

Sometimes we need to find the differences between two lists. It will also mean a mathematical subtraction in which the elements from the first list are removed if they are present in the second list. Duplicates are preserved. Below is the approach through which we can achieve this.We can use the Counter method from the collections module which will keep track of the count of elements. A straight mathematical subtraction gives the desired result. In the final result the number of occurrences of an element between the first and the second list will decide the elements.Examplefrom collections import Counter ...

Read More

Dividing two lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

The elements in tow lists can be involved in a division operation for some data manipulation activity using python. In this article we will see how this can be achieved.With zipThe zip function can pair up the two given lists element wise. The we apply the division mathematical operator to each pair of these elements. Storing the result into a new list.Example# Given lists list1 = [12, 4, 0, 24] list2 = [6, 3, 8, -3] # Given lists print("Given list 1 : " + str(list1)) print("Given list 2 : " + str(list2)) # Use zip res = ...

Read More
Showing 1331–1340 of 8,519 articles
« Prev 1 132 133 134 135 136 852 Next »
Advertisements