How-to articles, tricks, and solutions about PYTHON-2.X

How to return dictionary keys as a list in Python?

In Python, you can use the dict.keys() method to return a view object that contains the keys of a dictionary.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

This error is raised when trying to encode a Unicode string using the ASCII codec, and the string contains a character that is not within the ASCII range (0-127).

What is __future__ in Python used for and how/when to use it, and how it works

The __future__ module in Python allows you to enable new language features which are not compatible with the current version of Python.

What is the difference between dict.items() and dict.iteritems() in Python2?

In Python 2, dict.items() returns a list of the dictionary's key-value pairs, whereas dict.iteritems() returns an iterator over the dictionary's key-value pairs.