
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

3K+ Views
A list in python is a number of items placed with in [] which may or may not have same data types. It can also contain duplicates. In this article we will see how to extract only the unique values from a list.With append()In this approach we will first create a new empty list and then keep appending elements to this new list only if it is not already present in this new list. A for loop is used along with not in condition. It checks for the existence of the incoming element and it is appended only if it ... Read More

2K+ Views
A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the last element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index -1 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("Lastst Items from sublists:") for item in Alist: print((item[-1]))OutputRunning the above code gives us ... Read More

5K+ Views
A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("First Items from sublists:") for item in Alist: print((item[0]))OutputRunning the above code gives us ... Read More

15K+ Views
There may be situation when you need to get the first and last element of the list. The tricky part here is you have to keep track of the length of the list while finding out these elements from the lists. Below are the approaches which we can use to achieve this. But of course all the approaches involve using the index of the elements in the list.Using only indexIn any list the first element is assigned index value 0 and the last element can be considered as a value -1. So we apply these index values to the list ... Read More

414 Views
For many programs getting the keys from a dictionary is important input to be used by some other program which rely on this dictionary. In this article we are going to see how to capture the keys as a list.Using dict.keysThis is a very direct method of accessing the keys. This method is available as a in-built method.Example Live DemoAdict = {1:'Sun', 2:'Mon', 3:'Tue', 4:'Wed'} print("The given dictionary is : ", Adict) print(list(Adict.keys()))OutputRunning the above code gives us the following result −The given dictionary is : {1: 'Sun', 2: 'Mon', 3: 'Tue', 4: 'Wed'} [1, 2, 3, 4]Using *The ... Read More

560 Views
A Python list can also contain sublist. A sublist itself is a list nested within a bigger list. In this article we will see how to count the number of unique sublists within a given list.Using CounterCounter is a subclass of Dictionary and used to keep track of elements and their count. It is also considered as an unordered collection where elements are stored as Dict keys and their count as dict value. So in the below example we directly take a list which has sublists.Example Live Demofrom collections import Counter # Given List Alist = [['Mon'], ['Tue', 'Wed'], ['Tue', 'Wed']] ... Read More

964 Views
While using Python dictionary we may need to identify each element of the dictionary uniquely. For that we have to assign unique IDs to each of the element in the dictionary. In this article we will see how to assign the same unique Id to an element if it is repeated in a Python dictionary.With enumerate() and OrderedDict.fromkeys()The enumerate function expands a given dictionary by adding a counter to each element of the dictionary. Then we apply the OrderedDict.fromkeys() which will extract the same value of the counter from the dictionary hence eliminating the duplicates values of IDs.Example Live Demofrom collections ... Read More

874 Views
Suppose we have a list of numbers called nums, we have to find the number of times that the list changes from positive-to-negative or negative-to-positive slope.So, if the input is like [2, 4, 10, 18, 6, 11, 13], then the output will be 2, as it changes the direction at 10 (positive-to-negative), and then at 6 (negative-to-positive).To solve this, we will follow these steps −To solve this, we will follow these steps −for i in range 1 to size of nums - 1, doif nums[i-1] < nums[i] > nums[i+1] or nums[i-1] > nums[i] < nums[i+1], thencount := count + 1return ... Read More

191 Views
Suppose we have a list of numbers called cells; this list is representing sizes of different cells. Now, in each iteration, the two largest cells a and b interact according to these rules: So, If a = b, they both die. Otherwise, the two cells merge and their size becomes floor of ((a + b) / 3). We have to find the size of the last cell or return -1 if there's no cell is remaining.So, if the input is like [20, 40, 40, 30], then the output will be 16, in first iteration, 40 and 40 will die, then ... Read More

9K+ Views
Suppose we have a list of words, we have to concatenate them in camel case format.So, if the input is like ["Hello", "World", "Python", "Programming"], then the output will be "helloWorldPythonProgramming"To solve this, we will follow these steps −s := blank stringfor each word in words −make first letter word uppercase and rest lowercaseconcatenate word with sret := s by converting first letter of s as lowercasereturn retLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, words): s = "".join(word[0].upper() + word[1:].lower() for word in words) return ... Read More