Delete an element from a dictionary

The del statement removes an element: del d[key] Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary: def removekey(d, key): r = dict(d) del r[key] return r The … Read more

Why is it string.join(list) instead of list.join(string)?

It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: ‘_’.join([‘welcome’, ‘to’, ‘stack’, ‘overflow’]) ‘_’.join((‘welcome’, ‘to’, ‘stack’, ‘overflow’)) ‘welcome_to_stack_overflow’ Using something other than strings will raise the following error: TypeError: sequence item 0: expected str instance, int found

How to read a file line-by-line into a list?

This code will read the entire file into memory and remove all whitespace characters (newlines and spaces) from the end of each line: with open(filename) as file: lines = file.readlines() lines = [line.rstrip() for line in lines] If you’re working with a large file, then you should instead read and process it line-by-line: with open(filename) … Read more

Why is reading lines from stdin much slower in C++ than Python?

tl;dr: Because of different default settings in C++ requiring more system calls. By default, cin is synchronized with stdio, which causes it to avoid any input buffering. If you add this to the top of your main, you should see much better performance: std::ios_base::sync_with_stdio(false); Normally, when an input stream is buffered, instead of reading one … Read more

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