
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

942 Views
A python list can contain tuples as its elements. In this article we will explore how to access every nth element form the tuples that are present as the elements in the given tuple.Using indexWe can design a for loop to access the elements from the list with the in clause applied for nth index. Then we store the result into a new list.Example Live DemoAlist = [('Mon', '3 pm', 10), ('Tue', '12pm', 8), ('Wed', '9 am', 8), ('Thu', '6 am', 5)] #Given list print("Given list: ", Alist) # Use index res = [x[1] for x in Alist] ... Read More

491 Views
Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out only the positive numbers from a list of lists. In the result a new list will contain nested lists containing positive numbers.With for inHere we simply apply the mathematical operator to check for the value of elements in a list using a for loop. If the value is positive we capture it as a list and Outer for loop stores as a final list of lists.Example Live DemolistA = [[-9, -1, 3], [11, -8, -4, 434, 0]] ... Read More

533 Views
Many different types of data container can get mixed up in python. A list can have elements each of which is a tuple. In this article we will take such a list and find the frequency of element in the tuples which are themselves elements of a list.Using count and mapWe apply a lambda function to count through each of the first element in the tuples present in the list. Then apply a map function to arrive at the total count of the element we are searching for.Example Live Demo# initializing list of tuples listA = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', ... Read More

497 Views
A given list has many repeated items. We are interested in finding out the sum of the frequency of some such items which are repeated in the list. Below are the approaches how we can achieve this.With sumWe have two lists. One has the list of values and other has the values whose frequency needs to be checked from the first list. So we create a for loop to count the number of occurrences of the elements from the second list in the first list and then apply the sum function to get the final sum of frequency.Example Live Demochk_list= ['Mon', ... Read More

263 Views
Lets consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.Consider a list of lists given below.listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]In the abobe list we have elements which are lists with 3 elements. If I consider the first inner list tit has a, a, b at positions 0, 1, 2. Similarly for the 3rd list it is c, a, b at 0, ... Read More

6K+ Views
Consider two lists. The elements in the second list are numbers which needs to be considered as index position for elements of the first list. For this scenario we have the below python programs.With map and getitemWe can use the getitem magic method is used to access the list items. We can use it along with the map function, so that we get the result from first list which takes the elements from second list as its indics.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1, 3] print("Given list A:", listA) print("Given list B:", listB) ... Read More

3K+ Views
A piece of data may contain letters, numbers as well as special characters. If we are interested in extracting only the letters form this string of data, then we can use various options available in python.With isalphaThe isalpha function will check if the given character is an alphabet or not. We will use this inside a for loop which will fetch each character from the given string and check if it is an alphabet. The join method will capture only the valid characters into the result.Example Live DemostringA = "Qwer34^&t%y" # Given string print("Given string : ", stringA) # ... Read More

1K+ Views
While using python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers form letters which can be used for future calculations.With splitThe split functions splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and text. We will use that along with a for loop to capture eachExample Live DemolistA = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given ... Read More

582 Views
During data manipulation with Python , we may need to bring two lists together and equate the elements in each of them pair wise. Which means the element at index 0 from list 1 will be equated with element from index 0 of list2 and so on.With tupleThe tuple function will be leveraged to take elements form each list in sequence and matching them up. We first store the result in a temp string which has the pattern in which the output of matching up of the values form lists will be displayed.Example Live DemolistA = ['day1', 'day2', 'day3'] listB = ... Read More

513 Views
A lot of statistical data analysis tries to find the values which have maximum frequency in a given list of values. Python provides multiple approaches using which we can find such value form a given list. Below are the approaches.Using CounterThe Counter function from collections module has a options which can directly find the most common element in a given list. We have the most_common function to which we pass a parameter 1 for only one element with highest frequency and pass 2 if we need two elements which have highest frequency.Example Live Demofrom collections import Counter # Given list ... Read More