Found 26504 Articles for Server Side Programming

__init__ in Python

Tushar Sharma
Updated on 09-May-2023 15:45:34

557 Views

As we journey through the arena of Python, we regularly come across factors of the language that permits us to express our thoughts and intentions extra intuitively. One such detail is the init approach, a powerful constructor method that brings our custom training to existence. In this post, we'll explore the inner workings of Python's init method, illuminating its key position in initializing our custom instructions and respiratory lifestyles into our code. The Birth of Objects: The Init Method When we create a new instance of a custom class, Python invokes a unique method known as init to initialize the ... Read More

Swift Program to Sort a Dictionary By Values

Ankita Saini
Updated on 09-May-2023 17:18:18

914 Views

Swift supports a sorted() method to sort all the elements present in the given dictionary. This method sorts the key-value pairs of the dictionary according to the values. Syntax func sorted(by:) Here, the value of by parameters are − Greater than(>) − To sort the elements into descending order. Less than(

Swift Program to Search an Element in a Dictionary

Ankita Saini
Updated on 09-May-2023 11:51:09

3K+ Views

In a Swift dictionary, we can search for an element that is either a key or value with the help of contains() function. The contains() function will return a boolean value which represents whether the specified element(key or value) is found or not. Syntax dict.contains{$0.key == “KeyName”} Here, dict is the dictionary. The contains() function returns a boolean value which represents the result. Here we pass a closure in the function which will check if the current key is equal to the specified key or not and then return the result accordingly. Algorithm Step 1 − Create a ... Read More

Swift Program to Replace Elements in a Dictionary

Ankita Saini
Updated on 09-May-2023 11:49:30

363 Views

In Swift, a dictionary is an unordered collection of key-value pairs. So, to replace the value of a key Swift provides bracket notation or subscript notations. Using this notation we can also add new key-value pair in the dictionary if the specified pair does not exist. Syntax dict[keyName] = “Value” Here, dict is the dictionary. The keyName represents the key whose value you want to replace, and the Value represent the new value. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Display the original dictionary. Step 3 − Now replace the value of ... Read More

Swift Program to Remove Null from a Dictionary

Ankita Saini
Updated on 09-May-2023 11:43:32

824 Views

Sometimes a dictionary contains null values so to remove those null values Swift provides an in-built function named the filter() method. In the filter() method, we pass a closure which returns a boolean value indicating whether the key-value pair should be included in the resultant dictionary or not. Or we can say that if the second element(value) of a key-value pair is not nil, then it will be included in the resultant dictionary. Otherwise not. Syntax dict.filter{$0.1 != nil} Here, dict is the dictionary. The filter() function return key-value pairs which satisfy the given closure. The closure returns true ... Read More

Swift Program to Remove duplicate elements from Dictionary

Ankita Saini
Updated on 09-May-2023 11:41:07

871 Views

In Swift dictionary, we cannot have duplicate keys but can have duplicate values hence in this article we will remove duplicate values from the dictionary. So for that, we use contains() function. This function checks if the dictionary contains duplicate values or not. If the dictionary contains duplicate values, then it will remove them from the resultant dictionary. Syntax dict.values.contains(val) Here, dict is the resultant dictionary in which we will store unique key-value pairs and the contains() function compares two values with each other. Algorithm Step 1 − Create a dictionary named as myCityRank with key-value pairs. Step ... Read More

__getitem__ and __setitem__ in Python

Sarika Singh
Updated on 02-Sep-2025 13:16:24

4K+ Views

In Python, you can customize many operations like accessing or modifying elements of a list or dictionary using special methods. Two important methods that make your objects behave like lists or dictionaries are: __getitem__: called when you access an item using obj[key]. __setitem__: called when you assign a value using obj[key] = value. These are examples of dunder methods (short for "double underscore"). They are special methods in Python with names that begin and end with double underscores. When you use square bracket syntax on your object: obj[key] automatically triggers __getitem__ obj[key] = value automatically triggers __setitem__ ... Read More

Swift Program to Remove an Element from a Dictionary

Ankita Saini
Updated on 09-May-2023 11:38:25

1K+ Views

In Swift, we can remove an element from the specified dictionary using the removeValue() function. This function removes the specified key and its associated value from the given dictionary. Syntax Dict.removeValue(forKey: key) Here, the key represents the key which we wanted to remove from the dictionary along with its value, and the dict represents the dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Print the original dictionary. Step 3 − Remove a key-value pair whose key is 2 using the removeValue() function. Step 4 − Print the updated dictionary. Example In ... Read More

Swift Program to Remove all the elements from Dictionary

Ankita Saini
Updated on 09-May-2023 11:14:25

385 Views

In a dictionary, to remove all the elements from the dictionary Swift provides an inbuilt function named as removeAll() function. It will remove all the key-value pairs from the specified dictionary. Syntax dict.removeAll() Here, dict is the dictionary. The removeAll() function does not take any parameter and removes all the key-value pairs present in the specified dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Print the original dictionary. Step 3 − Now remove all the key-value pairs from the dictionary using removeAll() function. Step 4 − Print the output. Example In ... Read More

Swift Program to Print the keys and values of a Dictionary

Ankita Saini
Updated on 09-May-2023 11:12:44

1K+ Views

A dictionary is an unordered collection in which data is stored in the form of key-value pairs. To print the keys and values of a dictionary Swift supports the following methods − Using for-in loop Using properties Method 1: Using for-in Loop To print keys and values of the given dictionary we can use a for-in loop. The for-in loop iterates through each pair of the dictionary and displays them on the output screen. Syntax for(key, value) in dict{ print(“\(key) = \(value)”) } Here, the key represents the key and value represents the associated ... Read More

Advertisements