Programming Articles - Page 2201 of 3366

Python - Get the Index of first element greater than K

Pradeep Elance
Updated on 23-Dec-2019 10:25:05

2K+ Views

The values of items in a python list are not necessarily in any sorted order. More over there may be situation when we are interested only in certain values greater than a specific value. In this article we will see how we can get theUsing EnumerationUsing enumeration we get both the index and value of the elements in the list. Then we apply the greater than condition to get only the first element where the condition is satisfied. The next function goes through each list element one by one.Example Live DemoList = [21, 10, 24, 40.5, 11] print("Given list: " + ... Read More

Python - Get sum of tuples having same first value

Pradeep Elance
Updated on 23-Dec-2019 10:22:16

509 Views

Tuples are python collections or arrays which are ordered but unchangeable. If we get a number of tuples where the first element is the same, then we may have a scenario when we need to add the second elements of those tuples whose first elements are equal.Using map and for loopIn this method we will first consider a list made up of tuples. Then convert them to dictionary so that we can associate the elements in the tuple as key value pair. Then we apply the for loop with summing the value for each key o the dictionary. Finally use ... Read More

Python - geometry method in Tkinter

Pradeep Elance
Updated on 23-Dec-2019 10:11:32

9K+ Views

Python has capability to create GUI applications using the Tkinter library. The library provides many methods useful for GUI applications. The geometry method is a fundamental one which decides the size, position and some other attributes of the screen layout we are going to create.Example - 1In the below program we create a window of size 22x200 pixels using the geometry method. Then we add a button to it and decide button position in the window using the side and pady options.Examplefrom tkinter import * base = Tk() base.geometry('200x200') stud = Button(base, text = 'Tutorialspoint', font =('Courier', 14, 'bold')) stud.pack(side ... Read More

Python - Check if two lists have any element in common

Pradeep Elance
Updated on 23-Dec-2019 10:09:16

4K+ Views

During manipulating data using python lists, we come across a situation where we need to know if two lists are entirely different from each other or they have any element in common. This can be found out by comparing the elements in the two lists with the below approaches decribed.Using InIn a for loop we use the in clause to check in an element is present in the list or not. We will stretch this logic to compare the elements of the lists by choosing an element from first list and checking its presence in the second list. So we ... Read More

Getting screens height and width using Tkinter Python

Pradeep Elance
Updated on 23-Dec-2019 10:00:44

481 Views

Tkinter is the library which gives GUI programming capability to python programs. As part of GUI creation we need to create screen layouts of different sizes and depth. In this program we will see how to calculate the size of a screen in terms of pixels as well as in mm. We can also get the depth of the screen in pixels. There are various methods available as part of Tkinter which we use for this.Examplefrom tkinter import * # creating tkinter window base = Tk() #screen's length and width in pixels and mm length_1= base.winfo_screenheight() width_1= base.winfo_screenwidth() length_2 = ... Read More

How to use an ArrayList in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:40:27

2K+ Views

The lambda expression is an inline code that can implement a functional interface without creating an anonymous class. An ArrayList can be used to store a dynamically sized collection of elements.In the below program, We have removed the elements of an ArrayList whose age is less than or equal to 20 using the removeIf() method. This method introduced in Java 8 version to remove all elements from a collection that satisfy a condition.Syntaxpublic boolean removeIf(Predicate filter) The parameter filter is a Predicate. If the given predicate satisfies the condition, the element can be removed. This method returns the boolean value true if elements are removed, false otherwise.Exampleimport java.util.*; ... Read More

Python program to find the smallest number in a list

Pavitra
Updated on 23-Dec-2019 09:30:14

367 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given al list, we need to display the smallest number available in the listHere we can either sort the list and get the smallest element or use the built-in min() function to get the smallest element.Now let’s observe the concept in the implementation below −Example Live Demolist1 = [101, 120, 104, 145, 99] # sorting using built-in function list1.sort() print("Smallest element is:", list1[0])OutputSmallest element is: 99All the variables are declared in the local scope and their references are seen in the figure ... Read More

Python program to find the second maximum value in Dictionary

Pavitra
Updated on 11-Jul-2020 11:28:51

719 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two integers, we need to print the second maximum value in the dictionaryNow let’s observe the concept in the implementation below−Approach 1 − Using sorted() function by negative indexesExample Live Demo#input example_dict ={"tutor":3, "tutorials":15, "point":9, "tutorialspoint":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])Output15Approach 2 − Here we use sort method on the list and then access the second largest elementExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # using built-in sort method list1.sort() # ... Read More

Python program to find the second largest number in a list

Pavitra
Updated on 11-Jul-2020 11:30:42

4K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to display the second largest number in a list.There are three approaches to solve the problem−Approach 1 − We use the set() function & remove() functionExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now computing the max element by built-in method? print(max(new_list))Output32Approach 2 − We use sort() method and negative indexesExample Live Demolist1 = [11, 22, 1, 2, ... Read More

Python program to find occurrence to each character in given string

Pavitra
Updated on 11-Jul-2020 11:31:49

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string, we need to find the occurrence of each character in a given string.Here we will be discussing 3 approaches as discussed below:LApproach 1 − The brute-force approachExample Live Demotest_str = "Tutorialspoint" #count dictionary count_dict = {} for i in test_str:    #for existing characters in the dictionary    if i in count_dict:       count_dict[i] += 1    #for new characters to be added    else:       count_dict[i] = 1 print ("Count of all characters in Tutorialspoint ... Read More

Advertisements