1
\$\begingroup\$

Basically I have a 2d list containing movies. Movies are specified by name, version and description. For example

['Jaws', 1, 'Movie about sharks'] - where Jaws = name, 1 = version and Movie about sharks = description.

My result is a dictionary which contains a map between name and description. But name should be updated to contain the version for e.g

['Jaws', 2, 'Movie about more sharks'] - should now be :

{'JawsV2': 'Movie about more sharks'}

Is there a more pythonic way to do this?

def movie_version_map():
    movies =  [['Jaws', 1, 'Movie about sharks'], 
               ['Jaws', 2, 'Movie about more sharks'], 
               ['HarryPotter', 1, 'Movie about magic'], 
               ['HarryPotter', 4, 'Movie about more magic']]
    for movie in movies:
        if movie[1] != 1:
            movie[0] = ''.join([movie[0], 'V',str(movie[1])])

    newversion = dict([(movie[0],movie[2]) for movie in movies])
    return newversion

Output

{'Jaws': 'Movie about sharks', 'HarryPotter': 'Movie about magic', 'HarryPotterV4': 'Movie about more magic', 'JawsV2': 'Movie about more sharks'}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What if a movie was to be called 'somethingV1' ? \$\endgroup\$ – SylvainD Mar 30 '13 at 13:33
  • \$\begingroup\$ this is a case where if version is 1 leave the name as it is \$\endgroup\$ – user1741339 Mar 30 '13 at 13:53
1
\$\begingroup\$

I included the case in which version is 1

{'{}{}'.format(name, version > 1 and 'V'+str(version) or ''):\ 
description for name,version,description in movies}
\$\endgroup\$
8
\$\begingroup\$

A simple dict comprehension and use of str.format() will do the job here:

>>> {"{}V{}".format(name, version): description 
     for name, version, description in movies}
{'HarryPotterV1': 'Movie about magic', 
 'HarryPotterV4': 'Movie about more magic', 
 'JawsV1': 'Movie about sharks', 
 'JawsV2': 'Movie about more sharks'}

Or, in very old versions of Python where dict comprehensions don't exist, simple replace with dict() and a generator expression - e.g: dict(... for ... in moves).

Note that if this is just because you want to have the data as keys, you don't need to turn them into a string, a tuple can be a key too:

>>> {(name, version): description 
     for name, version, description in movies}
{('Jaws', 1): 'Movie about sharks', 
 ('HarryPotter', 4): 'Movie about more magic', 
 ('Jaws', 2): 'Movie about more sharks', 
 ('HarryPotter', 1): 'Movie about magic'}

This would be more appropriate where you don't need the strings, as it means you don't have to parse stuff out or create strings for keys, and makes the data easier to manipulate.

\$\endgroup\$
1
\$\begingroup\$

I suggest to use list comprehension as others did. However, to simplify reading, I am creating a separate function called make_title to deal with combining title and version.

def make_title(title, version):
    if version != 1:
        title = '{}V{}'.format(title, version)
    return title

def movie_version_map():
    movies =  [['Jaws', 1, 'Movie about sharks'], 
               ['Jaws', 2, 'Movie about more sharks'], 
               ['HarryPotter', 1, 'Movie about magic'], 
               ['HarryPotter', 4, 'Movie about more magic']]

    new_version = dict((make_title(title, version), description) for title, version, description in movies)
    return new_version
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.