
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

583 Views
Two lists are given. We need to find the index of the elements from the first list whose values match with the elements in the second list.With indexWe simply design follow to get the value of the element in the second list and extract the corresponding index from the first list.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = ['Tue', 'Fri'] # Given lists print("The given list: ", listA) print("The list of values: ", listB) # using indices res = [listA.index(i) for i in listB] # Result print("The Match indices list is : ", res)OutputRunning the above code gives ... Read More

1K+ Views
Given a Python list we want to find out only e e the last few elements.With slicingThe number of positions to be extracted is given. Based on that we design is slicing technique to slice elements from the end of the list by using a negative sign.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list : ", listA) # initializing N n = 4 # using list slicing res = listA[-n:] # print result print("The last 4 elements of the list are : ", res)OutputRunning the above code gives us the following result −Given list ... Read More

770 Views
A Python dictionary contains key value pairs. In this article we will see how to get the key of the element whose value is maximum in the given Python dictionary.With max and getWe use the get function and the max function to get the key.Example Live DemodictA = {"Mon": 3, "Tue": 11, "Wed": 8} print("Given Dictionary:", dictA) # Using max and get MaxKey = max(dictA, key=dictA.get) print("The Key with max value:", MaxKey)OutputRunning the above code gives us the following result −Given Dictionary: {'Mon': 3, 'Tue': 11, 'Wed': 8} The Key with max value: TueWith itemgetter and maxWith itemgetter function we get ... Read More

3K+ Views
Python dictionary contains key value pairs. In this article we aim to get the value of the key when we know the value of the element. Ideally the values extracted from the key but here we are doing the reverse.With index and valuesWe use the index and values functions of dictionary collection to achieve this. We design a list to first get the values and then the keys from it.Example Live DemodictA = {"Mon": 3, "Tue": 11, "Wed": 8} # list of keys and values keys = list(dictA.keys()) vals = list(dictA.values()) print(keys[vals.index(11)]) print(keys[vals.index(8)]) # in one-line print(list(dictA.keys())[list(dictA.values()).index(3)])OutputRunning the above code gives ... Read More

775 Views
When a Python list contains values like true or false and 0 or 1 it is called binary list. In this article we will take a binary list and find out the index of the positions where the list element is true.With enumerateThe enumerate function extracts all the elements form the list. We apply a in condition to check of the extracted value is true or not.Example Live DemolistA = [True, False, 1, False, 0, True] # printing original list print("The original list is : ", listA) # using enumerate() res = [i for i, val in enumerate(listA) if val] # ... Read More

353 Views
We have a tuple of strings. We are required to create a list of elements which are the first character of these strings in the tuple.With indexWe design a for loop to take each element and extract the first character by applying the index condition as 0. Then the list function converts it to a list.Example Live DemotupA = ('Mon', 'Tue', 'Wed', 'Fri') # Given tuple print("Given list : " ,tupA) # using index with for loop res = list(sub[0] for sub in tupA) # printing result print("First index charaters:", res)OutputRunning the above code gives us the following result −Given list ... Read More

1K+ Views
We have a list of tuple. We are required to find out that tuple which was maximum value in it. But in case more than one tuple has same value we need the first tuple which has maximum value.With itemgetter and maxWith itemgetter(1) we get all the value from index position 1 and then apply a max function to get the item with max value. But in case more than one result is returned, we apply index zero to get the first tuple with maximum element in it.Example Live Demofrom operator import itemgetter # initializing list listA = [('Mon', 3), ('Tue', ... Read More

982 Views
We have a list whose elements are dictionaries. We need to flatten it to get a single dictionary where all these list elements are present as key-value pairs.With for and updateWe take an empty dictionary and add elements to it by reading the elements from the list. The addition of elements is done using the update function.Example Live DemolistA = [{'Mon':2}, {'Tue':11}, {'Wed':3}] # printing given arrays print("Given array:", listA) print("Type of Object:", type(listA)) res = {} for x in listA: res.update(x) # Result print("Flattened object: ", res) print("Type of flattened Object:", type(res))OutputRunning the above code gives us the following ... Read More

542 Views
In this article we are required to find the first occurring non-zero number in a given list of numbers.With enumerate and nextWe sue enumerate to get the list of all the elements and then apply the next function to get the first non zero element.Example Live DemolistA = [0, 0, 13, 4, 17] # Given list print("Given list: " ,listA) # using enumerate res = next((i for i, j in enumerate(listA) if j), None) # printing result print("The first non zero number is at: ", res)OutputRunning the above code gives us the following result −Given list: [0, 0, 13, 4, 17] ... Read More

797 Views
Given a list of strings, lets find out the first non-empty element. The challenge is – there may be one, two or many number of empty strings in the beginning of the list and we have to dynamically find out the first non-empty string.With nextWe apply the next function to keep moving to the next element if the current element is null.Example Live DemolistA = ['', 'top', 'pot', 'hot', ' ', 'shot'] # Given list print("Given list: " ,listA) # using next() res = next(sub for sub in listA if sub) # printing result print("The first non empty string is : ... Read More