There is a list of file names. I am trying to create a dictionary based on it, so that the key is the time to create the file, and the value is the name of the file.

import os AllFileFull = ['file1.txt', 'file2.txt'] DictTimeFull = {os.path.getmtime(file_name):file_name for file_name in AllFileFull} 

When the script is run, the interpreter finds a syntax error, although there are no errors in the python 2.7.5 IDE and the code is executed.

 # python test-script.py File "test-script.py", line 5 DictTimeFull = {os.path.getmtime(file_name):file_name for file_name in AllFileFull} ^ SyntaxError: invalid syntax 

The question is how to rewrite this place?

  • Try to publish a minimal example that reproduces behavior - Timofei Bondarev
  • Thank you for correcting. I will try to follow the rules. - gberc

1 answer 1

Dictionary comprehensinons (constructions of the form {x.key:x.value for x in iterable} , allowing you to succinctly create and populate dictionaries) were introduced in Pyhton 2.7 so that developers can start porting code to Python 3. In Python 2.6, such constructions are not are supported. An analogue can serve as a loop filling the dictionary:

 DictTimeFull = {} for file_name in AllFileFull: DictTimeFull[os.path.getmtime(file_name)] = file_name 
  • one
    You can also do it via dict([(x.key, x.value) for x in iterable]) - BOPOH