Found 10784 Articles for Python

Python program to count the elements in a list until an element is a Tuple?

AmitDiwan
Updated on 11-Aug-2022 08:53:33

417 Views

In this article, we will count the elements in a list until an element is a Tuple. The list is the most versatile datatype available in Python, which can be written as a list of commaseparated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square ... Read More

Python program to count occurrences of an element in a tuple

AmitDiwan
Updated on 11-Aug-2022 08:48:53

985 Views

We will see how to count occurrences of an element in a Tuple. A tuple is a sequence of immutable Python objects. Let’s say we have the following input, with the occurrences of 20 to be checked − myTuple = (10, 20, 30, 40, 20, 20, 70, 80) The output should be − Number of Occurrences of 20 = 3 Count occurrence of an element in a Tuple using for loop In this example, we will count the occurrences of an element in a Tuple − Example def countFunc(myTuple, a): count = 0 for ele in myTuple: ... Read More

Python program to count the number of vowels using set in a given string

AmitDiwan
Updated on 11-Aug-2022 09:47:59

1K+ Views

We will count the number of vowels using set in a given string. Let’s say we have the following input − jackofalltrades The output should be the following, counting the number of vowels − 65 Count the number of vowels using set in a given string We will count the number of vowels using set in a give string − Example def vowelFunc(str): c = 0 # Create a set of vowels s="aeiouAEIOU" v = set(s) # Loop to traverse the alphabet in the given string for alpha in str: # If alphabet is present # in ... Read More

Python program to check if two lists have at least one common element

AmitDiwan
Updated on 11-Aug-2022 09:46:25

2K+ Views

To check if two lists have at least one common element, we will loop through list1 and list2 and check for atleast one common element in both the lists. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. Let’s say we have the following inputs i.e. 2 lists − [15, 45, 67, 30, 98] [10, 8, 45, 48, 30] The output should be the following ... Read More

Python program to check for URL in a string

Sarika Singh
Updated on 04-Apr-2023 11:47:24

2K+ Views

This article will teach you how to determine whether a string contains a URL or not. In Python, strings are collections of bytes that represent Unicode characters. You can use single or double quotes and everything enclosed in them is considered as a string. When given a string, we will first determine whether it contains a URL. If one is found, we will then print the URL. Using findall() method We will employ Python's regular expression concept to resolve this issue. Regular expressions are supported by the Python re package. Using a particular syntax defined in a pattern, a regular ... Read More

Python program to check whether two lists are circularly identical

Arushi
Updated on 30-Jul-2019 22:30:23

599 Views

Here two lists are given. Our task is to check and found weather two given lists are circularly identical or not. Example Input : A = [100, 100, 10, 10, 100] B = [100, 100, 100, 10, 10] Output : True Explanation True as when these elements in the list will circularly rotate then they would be similar to other given list Algorithm Step 1: Create First and Second List. Step 2: Then Lists are converted to map. Step 3: join () method is used for converting the ... Read More

Python Program to print unique values from a list

Sarika Singh
Updated on 23-Nov-2022 08:24:42

2K+ Views

List is a built-in data structure in python which functions like a dynamically sized array. Lists are created by putting elements in square brackets ‘[element 1, element 2, ….]’. Elements present in list are indexed and the indexing starts from [0]. List has the following properties − List is mutable meaning that elements can be added, removed or changed from a list after its creation. List is ordered, so adding new elements to the list will not change the order of existing elements. List allows for entry of duplicate elements since each element has a unique index. A single ... Read More

Python program to sort a tuple by its float element

Sarika Singh
Updated on 23-Nov-2022 07:33:43

1K+ Views

This article will demonstrate how write a Python program to sort a tuple (made up of float elements) using its float elements. Here, we'll look at both how to sort using the in-built sorted() function and how to sort using the in-place method of sorting. Input-Output Scenarios Following is an input and its output scenario determining the sorting of a tuple by its float element − Scenario-1 Input: tuple = [(‘Dengu’, ’54.865’), (‘Malaria’, ‘345.743’), (‘Corona’, ‘456.864’), (‘Typhoid’, ‘35.285’), (‘Jaundice’, ’83.367’)] Output: [(‘Corona’, ‘456.864’), (‘Malaria’, ‘345.743’), (‘Jaundice’, ’83.367’), (‘Dengu’, ’54.865’), (‘Typhoid’, ‘35.285’)] In the above scenario we can see that ... Read More

Python program to find k'th smallest element in a 2D array

Arushi
Updated on 30-Jul-2019 22:30:23

372 Views

One n×n user input integer matrix is given and the value of k. Our task is to find out k'th smallest element in a 2D array. Here we use heapq mudule.Heap queue (or heapq) in Python. In Python, it is available using “heapq” module. The technique of this module in python is that each time the smallest of heap element is popped (min heap).nsmallest () method is used to get n least values from a data frame or a series. Example Input Array is:: 10 20 20 40 15 45 40 30 32 33 30 50 ... Read More

Python program to generate all possible valid ID address from given string

Vikyath Ram
Updated on 30-Jul-2019 22:30:23

675 Views

String is given. String contains only digit. Our task is to check all possible valid IP address combinations. Here first we check the length of the string then split by ".". Then we check the different combination of ".". Example Input : "255011123222" It's not a valid IP address. Input : 255011345890 Valid IP address is 255.011.123.222 Algorithm Step 1: First check the length of the string. Step 2: Split the string by ".". We will place 3 dots in the given string. W, X, Y, and Z are numbers from 0-255 the numbers cannot be ... Read More

Advertisements