Snippets Collections
extension Encodable {
    func toDictionary() -> [String: Any]? {
        do {
            let encoder = JSONEncoder()
            let data = try encoder.encode(self)
            let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
            return dictionary
        } catch {
            print("Error converting to dictionary:", error)
            return nil
        }
    }
}
struct Person : Encodable {
    var name: String
    var age: Int
    var address: String?
}
    
    
let person = Person(name: "John Smith", age: 30, address: nil)
let dictionary = person.toDictionary()
print(dictionary) // Output: ["name": "John Smith", "age": 30]
    
    
    extension Encodable {
    func toDictionary() -> [String: Any]? {
        let mirror = Mirror(reflecting: self)
        var dictionary = [String: Any]()
        for child in mirror.children {
            guard let label = child.label else {
                continue
            }
            dictionary[label] = child.value
        }
        return dictionary
    }
}
# Import the itertools library 
# This library ships with Python 3 so you don't have to download anything
# It provides a bunch of tools for dealing with sequences and iteration
import itertools

# Create a new dictionary variable
myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

# Create a sliced dictionary
# dict() is used to convert the iterable result of itertools.islice() to a new dictionary
slicedDict = dict(itertools.islice(myDictionary.items(), 1 ,3))

print(slicedDict)

# Prints out 
# {   
#     'animal': 'cat', 
#     'colour': 'red'
# }
Dim resultSite As String =
    appEnums.OrderBy(Function(kvp) kvp.Key)
        .SkipWhile(Function(kvp) kvp.Key <= startSite)
        .Where(Function(kvp) kvp.Key <> mainSite AndAlso kvp.Key <> returnSite)
        .Select(Function(kvp) kvp.Value).FirstOrDefault()
        let dataToUpdate = [
            "certificationsList" : FieldValue.arrayUnion (
                [[ "company": certificate.company,
                   "title" : certificate.title,
                   "startDate" : startDate1,
                   "endDate" : endDate1
                ]])
              ]
        let dataToUpdate = [
            "languagesList" : FieldValue.arrayUnion(
                [[ "languageLevel": language.language_Level,
                "languageTitle" : language.language_Title  ]])
                                                    ]
fruit = {
  "elderberries": 1,
  "figs": 1,
  "apples": 2,
  "durians": 3,
  "bananas": 5,
  "cherries": 8,
  "grapes": 13
}

table_data = []
for k, v in fruit.items():
   table_data.append([k, v])
keys, values)) # {'a': 2, 'c': 4, 'b': 3}
 
 
#make a function: def is the keyword for the function:
def to_dictionary(keys, values):
 
 
#return is the keyword that tells program that function has to return value   
return dict(zip(keys, values))
 
  
 
# keys and values are the lists:
 
keys = ["a", "b", "c"]   
 
values = [2, 3, 4]
                                
                                
def minus_key(key, dictionary):

shallow_copy = dict(dictionary)

del shallow_copy[key]

return shallow_copy
                               
                                
>>> stuff = {‘name’ : ‘Zed’, ‘age’ : 39, ‘height’ : 6 * 12 +1}
>>. print(stuff [‘name’])
Zed
>>> print(stuff [‘age’])
39
>>> print(stuff [‘height’])
74
>>> stuff [‘city’] = “SF”
>>. print(stuff[‘city’])
SF
                                
                                
for (int i = 0; i < n; i++)
{
    if (valCount.ContainsKey(x))
        valCount[x]++;
    else
        valCount[x] = 1;
}
 
def merge_two_dicts(a, b):
 
 
   c = a.copy()   # make a copy of a
 
   c.update(b)    # modify keys and values of a with the ones from b
 
   return c
 
 
 
 
 
a = { 'x': 1, 'y': 2}
 
b = { 'y': 3, 'z': 4}
 
 
print(merge_two_dicts(a, b)) # {'y': 3, 'x': 1, 'z': 4}
 
 
month_conversion = {
“Jan” = “January”
“Feb” = “February”
“Mar” = “March”
“Apr” = “April”
“Jun” = “June”
}
# keys must be unique:
print(month_conversion[“Mar”])


Output”
           March
star

Fri Mar 31 2023 06:42:39 GMT+0000 (Coordinated Universal Time)

#ios #swift #dict #model #dictionary
star

Tue Dec 13 2022 19:47:27 GMT+0000 (Coordinated Universal Time) https://www.linuxscrew.com/python-dictionary-slice

#python #dictionary
star

Wed Jun 29 2022 18:02:56 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/10727622/vb-net-and-linq-query-on-a-dictionary

#vb.net #linq #dictionary
star

Wed Jun 15 2022 05:17:16 GMT+0000 (Coordinated Universal Time)

#ios #swift #update #dictionary #array
star

Tue Jun 14 2022 09:49:00 GMT+0000 (Coordinated Universal Time)

#ios #swift #update #dictionary #array
star

Sun May 02 2021 08:00:48 GMT+0000 (Coordinated Universal Time)

#python #list #dictionary
star

Wed Sep 09 2020 13:28:09 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/a/483833/6942743

#python #dictionary
star

Tue Apr 21 2020 11:45:29 GMT+0000 (Coordinated Universal Time) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #lists #dictionary
star

Tue Apr 21 2020 05:22:45 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary

#python #python #dictionary #del
star

Tue Apr 21 2020 05:10:22 GMT+0000 (Coordinated Universal Time) https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp/0321884914

#python #python #dictionary
star

Wed Apr 01 2020 08:37:24 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/60966491/how-to-clear-item-not-input-when-count-frequency-use-dictionary-c-sharp

#C# #c# #array #dictionary
star

Tue Mar 31 2020 05:32:45 GMT+0000 (Coordinated Universal Time)

#python #python #dictionary #mergedictionary #dict
star

Mon Mar 30 2020 12:10:17 GMT+0000 (Coordinated Universal Time) https://www.youtube.com/watch?v=rfscVS0vtbw&t=5s

#python #python #dictionary #keys

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension