All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Compare if two Maps are Equal in Golang

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...
Patcher56

How to Delete an Element from an Array in PHP

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...
Sambhav Khandelwal

How to Set Environment Variables in Python

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 ...
Sambhav Khandelwal

How to Get Key by Value in Dictionary in Python

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: #!...
aweis

How to Return Dictionary Keys as a List in Python

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/...
Sambhav Khandelwal

How to Remove Duplicates in List in Python

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...
pooriabt

How to Count the Items in the Map in Go

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...
Sambhav Khandelwal

How to Prettyprint a JSON File in Python

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...
Patcher56

How to Count the Occurrences of a List Item in Python

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...
Patcher56

How to Delete or Remove keys in a Map in Go

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...
Patcher56

How to Get a Slice of Keys from a Map in Go

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 ...
Sambhav Khandelwal

How to check if key exists in HashMap in Rust

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...
Unused

How to check if a map is empty in Go

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"...
Tomoki

How to convert string to json in Python

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...
pooriabt

How to check if key exists in a dictionary in Python

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} ...
Patcher56