Tutorial by Examples: dict

If we have this dictionary: set alpha {alice {items {}} bob {items {}} claudia {items {}} derek {items {}}} And want to add "fork" and "peanut" to Alice's items, this code won't work: dict lappend alpha alice items fork peanut dict get $alpha alice # => items {} items f...
--- person_table: - &person001 fname: homer lname: simpson role: dad age: 33 - &person002 fname: marge lname: simpson role: mom age: 34 - &person003 fname: pe...
Another straightforward way to go is to create a dictionary of functions: switch = { 1: lambda: 'one', 2: lambda: 'two', 42: lambda: 'the answer of life the universe and everything', } then you add a default function: def default_case(): raise Exception('No case found!') ...
Dictionaries map keys to values. car = {} car["wheels"] = 4 car["color"] = "Red" car["model"] = "Corvette" Dictionary values can be accessed by their keys. print "Little " + car["color"] + " " + car["model&q...
Given this setup code: var dict = new Dictionary<int, string>() { { 1, "First" }, { 2, "Second" }, { 3, "Third" } }; Use the Remove method to remove a key and its associated value. bool wasRemoved = dict.Remove(2); Executing this code remo...
A dictionary object has the method copy. It performs a shallow copy of the dictionary. >>> d1 = {1:[]} >>> d2 = d1.copy() >>> d1 is d2 False >>> d1[1] is d2[1] True
Creating a dictionary: set mydict [dict create a 1 b 2 c 3 d 4] dict get $mydict b ; # returns 2 set key c set myval [dict get $mydict $key] puts $myval # remove a value dict unset mydict b # set a new value dict set mydict e 5 Dictionary keys can be nested. dict set mycars mustang colo...
You might expect a Python dictionary to be sorted by keys like, for example, a C++ std::map, but this is not the case: myDict = {'first': 1, 'second': 2, 'third': 3} print(myDict) # Out: {'first': 1, 'second': 2, 'third': 3} print([k for k in myDict]) # Out: ['second', 'third', 'first'] Py...
set alpha {a 1 b 2 c 3} dict get $alpha b # => 2 dict get $alpha d # (ERROR) key "d" not known in dictionary If dict get is used to retrieve the value of a missing key, an error is raised. To prevent the error, use dict exists: if {[dict exists $alpha $key]} { set result [d...
A DataFrame can be created from a list of dictionaries. Keys are used as column names. import pandas as pd L = [{'Name': 'John', 'Last Name': 'Smith'}, {'Name': 'Mary', 'Last Name': 'Wood'}] pd.DataFrame(L) # Output: Last Name Name # 0 Smith John # 1 Wood Mary Missin...
You can iterate over the contents of a dictionary with dict for, which is similar to foreach: set theDict {abcd {ab cd} bcde {ef gh} cdef {ij kl}} dict for {theKey theValue} $theDict { puts "$theKey -> $theValue" } This produces this output: abcd -> ab cd bcde -> ef gh c...
Creating a list of KeyValuePair: Dictionary<int, int> dictionary = new Dictionary<int, int>(); List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(); list.AddRange(dictionary); Creating a list of keys: Dictionary<int, int> dictionary ...
The power of AppleScript lies in being able to automate many Mac applications. To find out what you can automate, you need to read an app's scripting dictionary. To do so, launch Script Editor, and select File > Open Dictionary… Once you choose an app, its dictionary will open up in a new win...
C++14 It is often useful to define classes or structures that have a variable number and type of data members which are defined at compile time. The canonical example is std::tuple, but sometimes is it is necessary to define your own custom structures. Here is an example that defines the structure ...
You can create extension methods to improve usability for nested collections like a Dictionary with a List<T> value. Consider the following extension methods: public static class DictListExtensions { public static void Add<TKey, TValue, TCollection>(this Dictionary<TKey, TColl...
import pandas as pd data = [ {'name': 'Daniel', 'country': 'Uganda'}, {'name': 'Yao', 'country': 'China'}, {'name': 'James', 'country': 'Colombia'}, ] df = pd.DataFrame(data) filename = 'people.csv' df.to_csv(filename, index=False, encoding='utf-8')
It is possible to create more complex loops with dictionaries. From vars: packages: - present: tree - present: nmap - absent: apache2 then the loop: - name: manage packages package: name={{ item.value }} state={{ item.key }} with_items: '{{ packages }}' Or, if you don't like ...
You can use a dictionary for a slightly more complex loop. - name: manage packages package: name={{ item.name }} state={{ item.state }} with_items: - { name: tree, state: present } - { name: nmap, state: present } - { name: apache2, state: absent }
' Just setting up the example Public Class A Public Property ID as integer Public Property Name as string Public Property OtherValue as Object End Class Public Sub Example() 'Setup the list of items Dim originalList As New List(Of A) originalList.Add(New A() With {...
Dictionaries are great for managing information where multiple entries occur, but you are only concerned with a single value for each set of entries — the first or last value, the mininmum or maximum value, an average, a sum etc. Consider a workbook that holds a log of user activity, with a script ...

Page 4 of 6