DMA57361
DMA57361

Reputation: 3690

Is it possible to directly extend multiple lists using a tuple of lists returned by a function?

I have a function that checks for file and directory changes within a given function and returns a tuple of lists, like this: addedFiles, removedFiles, addedDirs, removedDirs. Each of the named sublists in the tuple will be a list of strings (or empty). I need to append the results return by the function to local versions of these lists.

This heavily modified demonstrates the result I am after:

addedFiles, removedFiles, addedDirs, removedDirs = [],[],[],[]

for dir in allDirs:
    a,b,c,d = scanDir( dir )
    addedFiles.extend( a )
    removedFiles.extend( b )
    addedDirs.extend( c )
    removedDirs.extend( d )

But, I want to know if there is a better way to perform the section inside the for loop?
Like this it just feels a bit ... ugly.

Upvotes: 3

Views: 605

Answers (2)

Andreas
Andreas

Reputation: 560

How about:

 for a,b in zip(lists, extendwith):
     a.extend(b)

Upvotes: 6

eumiro
eumiro

Reputation: 212835

params = ('addedFiles', 'removedFiles', 'addedDirs', 'removedDirs')
paramDict = dict((param, []) for param in params)

for dir in allDirs:
    for param, data in zip(params, scanDir(dir)):
        paramDict[param].extend(data)

Now you have a dict of lists instead of four lists.

Upvotes: 2

Related Questions