Get Last Given Number of Items from an Array in Swift

Ankita Saini
Updated on 27-Jan-2023 12:30:54

181 Views

In this article, we will learn how to write a swift program to get the last given number of items from the array. An array is used to store elements of same data type in an order. So, here we are using the following methods to get the last given number of items from the array − Using user defined function Using suffix(_:) function Using suffix(from:) function Method 1: Using a user-defined function To get the last given number of items from the array we can create a function. This function will return an array containing the ... Read More

Get First Given Number of Items from Array in Swift

Ankita Saini
Updated on 27-Jan-2023 12:29:23

235 Views

In this article, we will learn how to write a swift program to get the first given number of items from the array. An array stores elements of the same data type in order. Therefore, here we use the following methods to get the first given number of items from the array − Using subscript with range Using prefix(_:) function Using prefix(upTo:) function Method 1: Using subscript with range To get the first given number of items from the array we can use subscript with different range operators like closed range operator, closed range operator with one-sided ranges, ... Read More

Fill an Array with a Specific Element in Swift

Ankita Saini
Updated on 27-Jan-2023 12:28:31

1K+ Views

In this article, we will learn how to write a swift program to fill an array with a specific element. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. In an array, every element has an index. The array index is start from 0 and goes up to N-1. Here N represent the total number of array elements. We can fill an array with a specified element using the following methods − Using user defined function Using ... Read More

Test If String Contains Element from List in Python

Vikram Chiluka
Updated on 27-Jan-2023 12:28:22

1K+ Views

In this article, we will learn how to check if a string contains an element from a list in python. Methods Used Using Nested For Loops Using List Comprehension Using any() function Using the find() function Example Assume we have taken an input string and input list. We will now check whether the input string contains at least any one input list element. Input inputString = "tutorialspoint is a best learning platform for coding" inputList = ['hello', 'tutorialspoint', 'python'] Output YES, the string contains elements from the input list In the above example, the ... Read More

Python Program to Uppercase Selective Indices

Vikram Chiluka
Updated on 27-Jan-2023 12:22:26

2K+ Views

In this article, we will learn a python program to convert the string elements to uppercase for the given selective indices. Methods Used The following are the various methods to accomplish this task − Using For Loop and upper() function Using List Comprehension Using index() & join() functions Example Assume we have taken an input string and a list containing index values. We will now convert the characters of an input string to uppercase at those given indices and print the resultant string. Input inputString = 'hello tutorialspoint python' indexList = [0, 6, 8, 10, 15, ... Read More

Test if Any Set Element Exists in List using Python

Vikram Chiluka
Updated on 27-Jan-2023 12:19:16

3K+ Views

In this article, we will learn how to check if any set element exists in the list in python. Methods Used Using any() function Using Bitwise & operator Using Counter(), filter() and lambda functions Example Assume we have taken an input set and input list. We will now check whether any input set element exists in the input list using the above methods. Input inputSet = {4, 8, 1, 3, 5, 7} inputList = [7, 15, 20] Output Checking whether any set element present in the input list: True In the above example, 7 exists ... Read More

Split a String by Custom Lengths in Python

Vikram Chiluka
Updated on 27-Jan-2023 12:08:51

3K+ Views

In this article, we will learn how to split a string by custom lengths in python. Methods Used The following are the various methods to accomplish this task − Using for loop and slicing Using List Comprehension, join() and next() functions Example Assume we have taken an input string and custom lengths list. We will now split an input string by input custom lengths using the above methods. Input inputString = 'hitutorialspoint' customLengths = [4, 1, 6, 5] Output ['hitu', 't', 'orials', 'point'] In the above example, firstly the input list is split based on the ... Read More

Sort a List of Names by Last Name in Python

Vikram Chiluka
Updated on 27-Jan-2023 12:05:17

2K+ Views

In this article, we will learn a python program to sort a list of names by last name. Methods Used Using lambda and sorted() functions Using lambda and sort() functions Example Assume we have taken an input list containing string elements. We will now sort the list elements in ascending order of the last names alphabetically using the above methods. Input Input List: ['sachin tendulkar', 'suresh raina', 'hardik pandya'] Output Input List after sorting by last names: ['hardik pandya', 'suresh raina', 'sachin tendulkar'] The last names of input list elements are tendulkar, raina, pandya i.e t, ... Read More

Replace All Words Except Given Word in Python

Vikram Chiluka
Updated on 27-Jan-2023 12:03:24

356 Views

In this article, we will learn how to replace all words except the given word in a string in python. Methods Used The following are the various methods to accomplish this task − Using For Loop, split() & join() Functions Using List Comprehension Example Assume we have taken an input string, input word, and the input character with which to be replaced. We will now replace all the words in an input string with the given input replace character expect the given word using the above methods. Input inputString = 'hello tutorialspoint python codes' inputWord = "tutorialspoint" ... Read More

Remove Numbers with Repeating Digits in Python

Vikram Chiluka
Updated on 27-Jan-2023 12:02:20

921 Views

In this article, we will learn how to remove numbers with repeating digits in python. Methods Used The following are the various methods to accomplish this task − Using list comprehension and set() function Using re module Using the Counter() function Example Assume we have taken an input list containing numbers. We will now remove all the numbers from the list containing repeating digits and return the resultant list using the above-mentioned methods. Input inputList = [3352, 8135, 661, 7893, 99] Output [8135, 7893] In the above input list, in the 1st element 3352, 3 ... Read More

Advertisements