How to sort a dictionary in Python by values?

Pythonic
Published on 28-Dec-2017 05:40:32
Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first ... Read More

How to sort a dictionary in Python by keys?

Pythonic
Published on 28-Dec-2017 05:45:11
Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first ... Read More

How to iterate through a dictionary in Python?

Pythonic
Published on 28-Dec-2017 05:49:04
There are two ways of iterating through a Python dictionary object. One is to fetch associated value for each key in keys() list. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> for k in D1.keys(): print (k, D1[k]) 1 a 2 b 3 c There ... Read More

How to print all the values of a dictionary in Python?

Pythonic
Published on 28-Dec-2017 05:56:31
Dictionary object possesses values() method which does this job for us. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> D1.values() dict_values(['a', 'b', 'c']) >>> list(D1.values()) ['a', 'b', 'c'] You can also get corresponding value by iterating through lidt of keys returned by keys() method of dictionary >>> L1 ... Read More

How to print all the keys of a dictionary in Python?

Pythonic
Published on 28-Dec-2017 06:00:17
Dictionary object possesses keys() method which does this job for us. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> D1.keys() dict_keys([1, 2, 3]) >>> list(D1.keys()) [1, 2, 3] iterable list object can be traversed using for loop >>> L1 = list(D1.keys()) >>> for i in ... Read More

How do we specify the buffer size when opening a file in Python?

Rajendra Dharmkar
Published on 27-Dec-2017 12:55:35
If you look at the function definition of open - open(name[, mode[, buffering]]), you'll see that it takes 3 arguments in Python 2, third one being buffering. The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use ... Read More

What does the 'U' modifier do when a file is opened using Python?

Rajendra Dharmkar
Published on 27-Dec-2017 12:53:09
In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". This is needed for crossplatform support as newlines on Unix os are represented by a single character \n while those on windows are ... Read More

What does the 'b' modifier do when a file is opened using Python?

Rajendra Dharmkar
Published on 27-Dec-2017 12:51:15
'b' modifier opens the file specified in binary mode. "binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are ... Read More

What are the modes a file can be opened using Python?

Rajendra Dharmkar
Published on 27-Dec-2017 12:49:20
Files in python can be opened in the following modes.ModeDescription'r'Read mode. (default)'w'Write mode. Creates a new file if it does not exist or truncates the file if it exists.'x'Open a file for exclusive creation. If the file already exists, the operation fails.'a'  Appending at the end of the file without ... Read More

How to check file last access time using Python?

Rajendra Dharmkar
Published on 27-Dec-2017 11:51:21
For getting the modification time for a file, you can use the os.path.getmtime(path). It is supported cross-platform.example>>> import os >>> print os.path.getmtime('my_file.txt') 1505928275.3081832
Advertisements