Found 10784 Articles for Python

Get first and last elements of a list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:26:46

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

Get dictionary keys as a list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:24:32

346 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

Count unique sublists within list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:21:49

417 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

Assign ids to each unique value in a Python list

Pradeep Elance
Updated on 09-Sep-2020 12:14:43

752 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

Changing Directions in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:28:44

645 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

Cell fusion in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:25:50

159 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

camelCase in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:21:45

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

Caesar Cipher in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:20:03

1K+ Views

Suppose we have a lowercase alphabet string s, and an offset number say k. We have to replace every letter in s with a letter k positions further along the alphabet. We have to keep in mind that when the letter overflows past a or z, it gets wrapped around the other side.So, if the input is like "hello", k = 3, then the output will be "khoor"To solve this, we will follow these steps −Define a function shift(). This will take ci := ASCII of (c) - ASCII of ('a')i := i + ki := i mod 26return character ... Read More

Buying Cars in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:18:08

348 Views

Suppose we have a list of prices of cars for sale, and we also have a budget k, we have to find the maximum number of cars we can buy.So, if the input is like [80, 20, 10, 30, 80], k = 85, then the output will be 3 as we can buy three cars with prices 20, 10, 40To solve this, we will follow these steps −count := 0sort the list pricesfor i in range 0 to size of prices, doif prices[i]

Boss Fight in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:15:57

451 Views

Suppose we have a binary list called fighters and another list of binary lists called bosses. In fighters list the 1 is representing a fighter. Similarly, in bosses list 1 representing a boss. That fighters can beat a boss’s row if it contains more fighters than bosses. We have to return a new bosses matrix with defeated boss rows removed.So, if the input is like fighters = [0,1,1]011000001011111then the output will be011111To solve this, we will follow these steps −fighter_cnt := sum of all elements of fightersresult := a new listfor each row in bosses, doif fighter_cnt

Advertisements