This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mike Davies
Recipients Mike Davies, paul.moore, steve.dower, tim.golden, zach.ware
Date 2019-03-18.02:12:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1552875140.48.0.232473204504.issue36334@roundup.psfhosted.org>
In-reply-to
Content
I wish to create a list of pathlib.Paths by combining two lists of pathlib.Paths. I use two for loops to make every combination.

However the output is not what one would expect (see no 'Program Files' is visible in the output).

########## INPUT:
pa = [   
    Path('C:/Program Files'),
    Path('C:/')
]
pb = [
    Path('/one/two/three.exe'),
    Path('/four.exe') 
    ]
print( [a/b for a in pa for b in pb] )

########### OUTPUT:
[WindowsPath('C:/one/two/three.exe'), WindowsPath('C:/four.exe'), WindowsPath('C:/one/two/three.exe'), WindowsPath('C:/four.exe')]

This is true whether I use for loops or list comprehensions.


To get the expected output I need to change the Paths to strings, combine them, then convert them back to paths like this:

########### INPUT:
print( [Path(str(a)+str(b)) for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/Program Files/one/two/three.exe'), WindowsPath('C:/Program Files/four.exe'), WindowsPath('C:/one/two/three.exe'), WindowsPath('C:/four.exe')]


Interestingly if I print only 'a' I get the expected answer:
########### INPUT:
print( [a for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/Program Files'), WindowsPath('C:/Program Files'), WindowsPath('C:/'), WindowsPath('C:/')]


And the same is true if I print only 'b':
########### INPUT:
print( [b for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('/one/two/three.exe'), WindowsPath('/four.exe'), WindowsPath('/one/two/three.exe'), WindowsPath('/four.exe')]



Additionally in some cases it does give the correct answer. Here is a similar example where the answer is correct:
########### INPUT:
pa = [Path('C:/'), Path('D:/')]
pb = [Path('a.exe'), Path('b.exe')]
print( [a/b for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/a.exe'), WindowsPath('C:/b.exe'), WindowsPath('D:/a.exe'), WindowsPath('D:/b.exe')]
History
Date User Action Args
2019-03-18 02:12:20Mike Daviessetrecipients: + Mike Davies, paul.moore, tim.golden, zach.ware, steve.dower
2019-03-18 02:12:20Mike Daviessetmessageid: <1552875140.48.0.232473204504.issue36334@roundup.psfhosted.org>
2019-03-18 02:12:20Mike Davieslinkissue36334 messages
2019-03-18 02:12:20Mike Daviescreate