Access an arbitrary element in a dictionary in Python

On Python 3, non-destructively and iteratively: next(iter(mydict.values())) On Python 2, non-destructively and iteratively: mydict.itervalues().next() If you want it to work in both Python 2 and 3, you can use the six package: six.next(six.itervalues(mydict)) though at this point it is quite cryptic and I’d rather prefer your code. If you want to remove any item, do: … Read more

Error: ” ‘dict’ object has no attribute ‘iteritems’ “

As you are in python3 , use dict.items() instead of dict.iteritems() iteritems() was removed in python3, so you can’t use this method anymore. Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated: Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues(). Instead: use dict.items(), dict.keys(), and dict.values() respectively.

Python: Checking if a ‘Dictionary’ is empty doesn’t seem to work

Empty dictionaries evaluate to False in Python: >>> dct = {} >>> bool(dct) False >>> not dct True >>> Thus, your isEmpty function is unnecessary. All you need to do is: def onMessage(self, socket, message): if not self.users: socket.send(“Nobody is online, please use REGISTER command” \ ” in order to register into the server”) else: … Read more

Merging dictionaries in C#

This partly depends on what you want to happen if you run into duplicates. For instance, you could do: var result = dictionaries.SelectMany(dict => dict) .ToDictionary(pair => pair.Key, pair => pair.Value); That will throw an exception if you get any duplicate keys. EDIT: If you use ToLookup then you’ll get a lookup which can have … Read more

How to iterate (keys, values) in JavaScript? [duplicate]

tl;dr In ECMAScript 2017, just call Object.entries(yourObj). In ECMAScript 2015, it is possible with Maps. In ECMAScript 5, it is not possible. ECMAScript 2017 ECMAScript 2017 introduced a new Object.entries function. You can use this to iterate the object as you wanted. ‘use strict’; const object = {‘a’: 1, ‘b’: 2, ‘c’ : 3}; for … Read more

How to do associative array/hashing in JavaScript

Use JavaScript objects as associative arrays. Associative Array: In simple words associative arrays use Strings instead of Integer numbers as index. Create an object with var dictionary = {}; JavaScript allows you to add properties to objects by using the following syntax: Object.yourProperty = value; An alternate syntax for the same is: Object[“yourProperty”] = value; … Read more

How can I use pickle to save a dict (or any other Python object)?

Try this: import pickle a = {‘hello’: ‘world’} with open(‘filename.pickle’, ‘wb’) as handle: pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL) with open(‘filename.pickle’, ‘rb’) as handle: b = pickle.load(handle) print(a == b) There’s nothing about the above solution that is specific to a dict object. This same approach will will work for many Python objects, including instances of arbitrary classes … Read more

Rename a dictionary key

For a regular dict, you can use: mydict[k_new] = mydict.pop(k_old) This will move the item to the end of the dict, unless k_new was already existing in which case it will overwrite the value in-place. For a Python 3.7+ dict where you additionally want to preserve the ordering, the simplest is to rebuild an entirely … Read more

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