How to merge two dictionaries with same key names [duplicate]

If you want a merged copy that does not alter the original dicts and watches for name conflicts, you might want to try this solution: #! /usr/bin/env python3 import copy import itertools def main(): dict_a = dict(a=[1], b=[2]) dict_b = dict(b=[3], c=[4]) complete_merge = merge_dicts(dict_a, dict_b, True) print(complete_merge) resolved_merge = merge_dicts(dict_a, dict_b, False) print(resolved_merge) def … Read more

How to create a new Dictionary from an IReadOnlyDictionary?

Dictionary<TKey, TValue> is able to implement IReadOnlyDictionary<TKey, TValue>, because any code that takes the latter is promising not the modify the dictionary, which is a subset of the functionality the former provides. But consider the other direction. You start with an IReadOnlyDictionary<TKey, TValue>, and you try to turn it into a regular Dictionary<TKey, TValue>. Now, … Read more

map versus mapM behavior

As I understand it, the function “executes” each element in the list which must be an “action” (IO monad). That’s true for IO, but in your code example you don’t use the IO monad, you use the list monad (the function you give to mapM returns a list ([x]), not an IO). mapM is defined … Read more

Modify list and dictionary during iteration, why does it fail on dict?

I think the reason is simple. lists are ordered, dicts (prior to Python 3.6/3.7) and sets are not. So modifying a lists as you iterate may be not advised as best practise, but it leads to consistent, reproducible, and guaranteed behaviour. You could use this, for example let’s say you wanted to split a list … Read more

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

toArray() returns an Object[], regardless of generics. You could use the overloaded variant instead: String[] str = map1.keySet().toArray(new String[map1.size()]); Alternatively, since a Set‘s toArray method gives no guarantee about the order, and all you’re using the array for is printing out the values, you could iterate the keySet() directly: for (String str: map1.keySet()) { System.out.println(str); … 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

update dictionary with dynamic keys and values in python

Remove the following line: mydic = {i : o[“name”]} and add the following before your loop: mydic = {} Otherwise you’re creating a brand new one-element dictionary on every iteration. Also, the following: mydic.update({i : o[“name”]}) is more concisely written as mydic[i] = o[“name”] Finally, note that the entire loop can be rewritten as a … Read more

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