Found 10784 Articles for Python

Add only numeric values present in a list in Python

Pradeep Elance
Updated on 09-Jul-2020 12:38:59

907 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

Accessing Key-value in a Python Dictionary

Pradeep Elance
Updated on 09-Jul-2020 12:37:20

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

Accessing index and value in a Python list

Pradeep Elance
Updated on 09-Jul-2020 12:34:46

2K+ 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

Python Grouped Flattening of list

Hafeezul Kareem
Updated on 07-Jul-2020 09:04:25

99 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

Python Group by matching second tuple value in list of tuples

Hafeezul Kareem
Updated on 07-Jul-2020 09:03:26

394 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

Python Grayscaling of Images using OpenCV

Hafeezul Kareem
Updated on 07-Jul-2020 09:00:25

3K+ Views

In this tutorial, we are going to learn how to change the grayscaling of an image using Grayscaling is the process of changing the images from different colour spaces like RGB, CMYK, etc.., to shades of gray. Install the OpenCV module if you didn't install it before.pip install opencv-pythonAfter installing the OpenCV module. Follow the below steps to write the code.Import the cv2 module.Read the image with cv2.imread(image_path) and store it in a variable.Convert the image colour scale using cv2.cvtColor(image, cv2.COLOR_BGR1GRAY) and store it in a variable.Show the image using cv2.imshow(image).Wait until any key press to exit using the cv2.waitKey().Destroy ... Read More

Python Getting started with psycopg2-PostgreSQL

Hafeezul Kareem
Updated on 07-Jul-2020 08:57:48

627 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

Python How to copy a nested list

Hafeezul Kareem
Updated on 07-Jul-2020 08:53:49

365 Views

In this tutorial, we are going to see different ways to copy a nested list in Python. Let's see one by one.First, we will copy the nested list using loops. And it's the most common way.Example Live Demo# initializing a list nested_list = [[1, 2], [3, 4], [5, 6, 7]] # empty list copy = [] for sub_list in nested_list:    # temporary list    temp = []    # iterating over the sub_list    for element in sub_list:       # appending the element to temp list       temp.append(element)    # appending the temp list to copy ... Read More

Python Indices of numbers greater than K

Hafeezul Kareem
Updated on 07-Jul-2020 08:52:46

441 Views

In this tutorial, we are going to find the indices of the numbers that are greater than the given number K. Let's see the different ways to find them.A most common way to solve the problem is using the loops. Let's see the steps to solve the problem.Initialize the list and K.Iterate over the list using its length.If you find any number greater than K, then print the current index.Example Live Demo# initializing the list and K numbers = [3, 4, 5, 23, 12, 10, 16] K = 10 # iterating over thAe list for i in range(len(numbers)):    # checking ... Read More

Python Indexing a sublist

Hafeezul Kareem
Updated on 07-Jul-2020 08:51:13

2K+ Views

In this tutorial, we are going to write a program that finds the index of a sublist element from the list. Let's see an example to understand it clearly.Inputnested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]OutputIndex of 7:- 2 Index of 5:- 1 Index of 3:- 0Let's see the simple and most common way to solve the given problem. Follow the given steps solve it.Initialize the list.Iterate over the list using the index.Iterate over the sub list and check the element that you want to find the index.If we find the element then print and break itExample Live ... Read More

Advertisements