Python Articles

Page 123 of 852

camelCase in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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 −Exampleclass Solution:    def solve(self, words):       s = "".join(word[0].upper() + word[1:].lower() for word in words)       return s[0].lower() ...

Read More

Cell fusion in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 228 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

Changing Directions in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 986 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

Assign ids to each unique value in a Python list

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 1K+ 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.Examplefrom collections import ...

Read More

Count unique sublists within list in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 607 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.Examplefrom collections import Counter # Given List Alist = [['Mon'], ['Tue', 'Wed'], ['Tue', 'Wed']] print(Counter(str(elem) ...

Read More

Get first element of each sublist in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 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.ExampleAlist = [['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 the ...

Read More

Get last element of each sublist in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 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.ExampleAlist = [['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 the ...

Read More

Get unique values from a list in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 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

Employee Importance in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 304 Views

Suppose we have a data structure of employee information, there are employee's unique id, his importance value and his direct subordinates' id. As an example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. And suppose their importance values are 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []].So, if we have the employee information of a company, and an employee id, we have to find the total importance value of ...

Read More

Kth Largest Element in a Stream in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 621 Views

Suppose we want to design a class to find the kth largest element in a stream. It is the kth largest element in the sorted order, not the kth distinct element.The KthLargest class will have a constructor which accepts an integer k and an array nums, that will contain initial elements from the stream. For each call to the method KthLargest.add, will return the element representing the kth largest element in the stream.So, if the input is like k = 3, initial elements = [4, 5, 8, 2], then call add(3), add(5), add(10), add(9), add(4). , then the output will ...

Read More
Showing 1221–1230 of 8,519 articles
« Prev 1 121 122 123 124 125 852 Next »
Advertisements