Testing the equivalence of maps in Go.
reflect.DeepEqual Function
It first checks if both maps are nil, then it checks if they have the same length before finally checking to see if they have the same set of (key, value) pairs.
package main
impo...
In PHP, there are 3 ways to delete an element from an array.
Using unset Function
The unset(mixed $var, mixed ...$vars): void function destroys the specified variables. For example,
$arr = ["a", "b", "d"];
unset($arr[1]);
print_r($arr);
Arr...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
In Python, there are 3 ways to get key by value in dictionary.
Using keys Function
Basically, it separates the dictionary's values in a list, finds the position of the value you have, and gets the key at that position.
The following example:
#!...
In Python, there are 3 ways to return dictionary keys as a list.
Using keys Function
The dict.keys() function returns a view object that displays a list of all the keys in the dictionary in order of insertion.
See the following example:
#!/usr/...
In Python, there are 3 ways to remove duplicates in list.
Using Built-in set Function
The built-in set() function returns a new set object, optionally with elements taken from iterable. If you later need a real list again, you can similarly pass the...
In Golang, using the len function is the easiest way to count the items in the map structure.
Using len Function
The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result...
In Python, there are 2 ways to prettyprint a JSON file.
Using json module
The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by:
#!/us...
In Python, there are 3 ways to count the occurrences of a list item.
Using count Method
The str.count() method returns the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpr...
In Golang, using the delete function is the easiest way to delete keys in a map.
Using delete Function
Syntax
The syntax of the delete function is shown below.
func delete(m map[Type]Type1, key Type)
delete Usage
The delete built-in funct...
In Golang, there are 3 ways to get a slice of keys from a map.Using For Range
The easiest way to get keys of map in Golang:
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 1,
"c": 1,
}
// minimize ...
In Rust, there are 2 ways to check if key exists in HashMap.
Using HashMap::contains_key Method
The contains_key method return true if the map contains a value for the specified key.
use std::collections::HashMap;
fn main() {
let mut map = Hash...
2 ways to check if a map is empty in Golang.Using len Function
You can use the len function to check if a map is empty. it returns the length of v, according to its type.
len(s) map[K]T: map length (number of defined keys)
package main
import "fmt"...
Converting Json to string in Python.Decoding JSON
To convert string to json in Python, use the json.loads() function. The json.loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements. The...
Check if a given key already exists in a dictionary.Using the in operator
In this method, we use the membership operator in. This operator is used to check if one value is a member of another. It returns a boolean value.
dict = {"GMT": 0, "BST": 1}
...