How to get a random value from dictionary?

One way would be: import random d = {‘VENEZUELA’:’CARACAS’, ‘CANADA’:’OTTAWA’} random.choice(list(d.values())) EDIT: The question was changed a couple years after the original post, and now asks for a pair, rather than a single item. The final line should now be: country, capital = random.choice(list(d.items()))

Looking for ALT+LeftArrowKey solution in zsh

Run cat then press keys to see the codes your shortcut send. (Press Ctrl+C to kill the cat when you’re done.) For me, (ubuntu, konsole, xterm) pressing Alt+← sends ^[[1;3D, so i would put in my .zshrc bindkey “^[[1;3C” forward-word bindkey “^[[1;3D” backward-word (Actually I prefer to use Ctrl + arrow to move word by … Read more

How to get key names from JSON using jq

You can use: jq ‘keys’ file.json Complete example $ cat file.json { “Archiver-Version” : “Plexus Archiver”, “Build-Id” : “”, “Build-Jdk” : “1.7.0_07”, “Build-Number” : “”, “Build-Tag” : “”, “Built-By” : “cporter”, “Created-By” : “Apache Maven”, “Implementation-Title” : “northstar”, “Implementation-Vendor-Id” : “com.test.testPack”, “Implementation-Version” : “testBox”, “Manifest-Version” : “1.0”, “appname” : “testApp”, “build-date” : “02-03-2014-13:41”, “version” : … Read more

How to print a dictionary’s key?

A dictionary has, by definition, an arbitrary number of keys. There is no “the key”. You have the keys() method, which gives you a python list of all the keys, and you have the iteritems() method, which returns key-value pairs, so for key, value in mydic.iteritems() : print key, value Python 3 version: for key, … Read more

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