Trying to combine (concatenate) elements from 3 lists into one, new list

Question:

I have 3 lists that contain Town, Range and Section (These are also the names of the lists) information. For example, List Town has ‘N14’…’N20’, List Range has ‘E4’…’E7′ and List Section has ’01’…’36’. I want to be able to put all possible combinations from the three lists into one new list, named AOI, like this ‘N14E401’….’N20E732’ (727 possible combinations). This is for a arcpy script that is already written, and working, that will use raw_input prompts (the above combinations) that will be then used as the AOI that will do some geoprocessing (not important as that part of the script works fine). I just want to make the AOI selection easier as the way I have it set up now, the user must input the Town, Range and Section information as individual raw_inputs in three separate steps.

Thanks in advance. I would have put this on the arcpy specific area but it seems more of a python question than an arcpy question.
I am a complete python noob and have been teaching myself scripting so…be gentle, kind readers.

Asked By: meh

||

Answers:

What you are trying to achieve is the Cartesian product of 3 lists. This can easily be achieved by using itertools.product

Off-course you would not get the O/P as you depicted instead you will get a list of tuples, but then again joining the list of tuples would be trivial. For each of the tuples you need to invoke str.join

You may either want to loop through the tuples, join the list while incrementally appending , or better use List comprehension

Answered By: Abhijit

Why not just use some simple for loops?

AOI = []
for t in Town:
    for r in Range:
        for s in Section:
            AOI.append(t + r + s)
Answered By: Evil Genius
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.