How to append enumerable collection to an existing list in C#

Well, something will have to loop… but in LINQ you could easily use the Concat and ToList extension methods: var bigList = list1.Concat(list2).Concat(list3).ToList(); Note that this will create a new list rather than appending items to an existing list. If you want to add them to an existing list, List<T>.AddRange is probably what you’re after: … Read more

How to sort a dictionary based on a list in python

The naive way, sorting the list of (key, value) tuples, using the sorted() function and a custom sort key (called for each (key, value) pair produced by dict.items())): sorted(a.items(), key=lambda pair: a_list.index(pair[0])) The faster way, creating an index map first: index_map = {v: i for i, v in enumerate(a_list)} sorted(a.items(), key=lambda pair: index_map[pair[0]]) This is … Read more

Python – Flatten the list of dictionaries

You can do the following, using itertools.chain: >>> from itertools import chain # timeit: ~3.40 >>> [dict(chain(*map(dict.items, d.values()))) for d in data] [{‘l’: ‘Apple’, ‘b’: ‘Milk’, ‘d’: ‘Meatball’, ‘favourite’: ‘coke’, ‘dislike’: ‘juice’}, {‘l’: ‘Apple1’, ‘b’: ‘Milk1’, ‘dislike’: ‘juice3’, ‘favourite’: ‘coke2’, ‘d’: ‘Meatball2’}] The usage of chain, map, * make this expression a shorthand for the … Read more

How to pass List of class to List of Interface?

If your code is simply iterating over the sequence inside the method (not adding, removing, or accessing by index), change your method to one of the following DoSomething(IEnumerable<IMyInterface> sequence) DoSomething<T>(IEnumerable<T> sequence) where T : IMyInterface The IEnumerable<> interface is covariant (as of .NET 4) (first option). Or you could use the latter signature if using … Read more

readonly keyword does not make a List ReadOnly?

The modifier readonly means that the value cannot be assigned except in the declaration or constructor. It does not mean that the assigned object becomes immutable. If you want your object to be immutable, you must use a type that is immutable. The type ReadOnlyCollection<T> that you mentioned is an example of a immutable collection. … Read more

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