LINQ query to return a Dictionary

Use the ToDictionary method directly. var result = // as Jon Skeet pointed out, OrderBy is useless here, I just leave it // show how to use OrderBy in a LINQ query myClassCollection.OrderBy(mc => mc.SomePropToSortOn) .ToDictionary(mc => mc.KeyProp.ToString(), mc => mc.ValueProp.ToString(), StringComparer.OrdinalIgnoreCase);

Python creating a dictionary of lists

You can use defaultdict: >>> from collections import defaultdict >>> d = defaultdict(list) >>> a = [‘1’, ‘2’] >>> for i in a: … for j in range(int(i), int(i) + 2): … d[j].append(i) … >>> d defaultdict(<type ‘list’>, {1: [‘1’], 2: [‘1’, ‘2’], 3: [‘2’]}) >>> d.items() [(1, [‘1’]), (2, [‘1’, ‘2’]), (3, [‘2’])]

How do I convert this list of dictionaries to a csv file?

import csv to_csv = [ {‘name’: ‘bob’, ‘age’: 25, ‘weight’: 200}, {‘name’: ‘jim’, ‘age’: 31, ‘weight’: 180}, ] keys = to_csv[0].keys() with open(‘people.csv’, ‘w’, newline=””) as output_file: dict_writer = csv.DictWriter(output_file, keys) dict_writer.writeheader() dict_writer.writerows(to_csv)

I’m getting Key error in python

A KeyError generally means the key doesn’t exist. So, are you sure the path key exists? From the official python docs: exception KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys. For example: >>> mydict = {‘a’:’1′,’b’:’2′} >>> mydict[‘a’] ‘1’ >>> mydict[‘c’] Traceback (most recent call last): File … Read more

What is the difference between a map and a dictionary?

Two terms for the same thing: “Map” is used by Java, C++ “Dictionary” is used by .Net, Python “Associative array” is used by PHP “Map” is the correct mathematical term, but it is avoided because it has a separate meaning in functional programming. Some languages use still other terms (“Object” in Javascript, “Hash” in Ruby, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)