
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
Found 10476 Articles for Python

236 Views
Suppose we have a list of lists in which each sublist has two elements. One element of each sublist is common across many other subjects of the list. We need to create a final list which will show sublists grouped by common elements.With set and mapIn the given list the first element is a string and the second element is a number. So we create a temp list which will hold the second element of each sublist. Then we compare is sublist with each of the element in the temp list and designer follow to group them.Example Live DemolistA = [['Mon', ... Read More

847 Views
The given task is to perform an alternate summation on a Python List. For example, adding the elements at even indices (0, 2, 4) or odd indices (1, 3, 5), depending on the requirement. This means we need to add every other element from the given list. Let us see an input scenario - Scenario Input: [11, 22, 33, 44, 55] Output: 99 Explanation: Here, we are going to add the elements at the even indices by using the slicing [::2]. Alternate Element Summation Using sum() Function The alternate element summation in a list (Python) can ... Read More

179 Views
We have a list whose elements are numeric. Many elements are present multiple times. We want to create sub list so the frequency of each element along with the elements itself.With for and appendIn this approach we will compare each element in the list with every other elements after it. If there is a match then count will be incremented and both the element and the count will be made into a subsist. List will be made which should contain subsists showing every element and its frequency.Example Live Demodef occurrences(list_in): for i in range(0, len(listA)): a = ... Read More

1K+ Views
We have a Python list which contains both string and numbers. In this article we will see how to sum up the numbers present in such list by ignoring the strings.With filter and isinstanceThe isinstance function can be used to filter out only the numbers from the elements in the list. Then we apply and the sum function and get the final result.Example Live DemolistA = [1, 14, 'Mon', 'Tue', 23, 'Wed', 14, -4] #Given dlist print("Given list: ", listA) # Add the numeric values res = sum(filter(lambda i: isinstance(i, int), listA)) print ("Sum of numbers in listA: ", res)OutputRunning the ... Read More

5K+ Views
While analyzing data using Python data structures we will eventually come across the need for accessing key and value in a dictionary. There are various ways to do it in this article we will see some of the ways.With for loopUsing a for loop we can access both the key and value at each of the index position in dictionary as soon in the below program.Example Live DemodictA = {1:'Mon', 2:'Tue', 3:'Wed', 4:'Thu', 5:'Fri'} #Given dictionary print("Given Dictionary: ", dictA) # Print all keys and values print("Keys and Values: ") for i in dictA : print(i, dictA[i])OutputRunning the above code ... Read More

3K+ Views
When we use a Python list, will be required to access its elements at different positions. In this article we will see how to get the index of specific elements in a list.With list.IndexThe below program sources the index value of different elements in given list. We supply the value of the element as a parameter and the index function returns the index position of that element.Example Live DemolistA = [11, 45, 27, 8, 43] # Print index of '45' print("Index of 45: ", listA.index(45)) listB = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Print index of 'Wed' print("Index of ... Read More

175 Views
In this tutorial, we are going to write a program that flattens a list that contains sub-lists. Given number flatten the sublists until the given number index as parts. Let's see an example to understand it clearly.Inputlists = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] number = 2Output[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]Let's see the steps to solve the problem.Initialize the list and number.Initialize an empty list.Iterate over the list with range(0, len(lists), number.Get the sublists using slicing lists[i:number].Iterate over the sublists and append the resultant list to the result list.Print the result.Example# ... Read More

477 Views
In this tutorial, we are going to write a program that groups all the tuples from a list that have same element as a second element. Let's see an example to understand it clearly.Input[('Python', 'tutorialspoints'), ('Management', 'other'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints'), ('Social', 'other'), ('Business', 'other')]Output{'tutorialspoint': [('Python', 'tutorialspoints'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints')], 'other’: [('Management', 'other'), ('Social', 'other'), ('Business', 'other')]}We have to group the tuples from the list. Let's see the steps to solve the problem.Initiate a list with required tuples.Create an empty dictionary.Iterate through the list of tuples.Check if the second element of the tuple is already present in the dictionary ... Read More

4K+ Views
Grayscaling an image includes converting a color image into a grayscale image. By using OpenCV library in various methods we can convert the color image with (multiple color channels) into a grayscale image (with a single channel). Some of the common methods for greyscaling an image using OpenCV are as follows. cv2.cvtColor(): This function is used to convert an image from one space color to another. Pixel ... Read More

919 Views
In this tutorial, we are going to learn how to use PostgreSQL with Python. You have to install certain thing before going into the tutorial. Let's install them.Install the PostgreSQL with the guide..Install the Python module psycopg2 for PostgreSQL connection and working. Run the command to install it.pip install psycopg2Now, open the pgAdmin. And create a sample database. Next, follow the below steps to get started with database operations.Import the psycopg2 module.Store the database name, username, and password in separate variables.Make a connection to the database using psycopg2.connect(database=name, user=name, password=password) method.Instantiate a cursor object to execute SQL commands.Create queries and ... Read More