0

I'm constructing dictionaries lists in Python, with aim to iterate through each one individually and concatenate all different combinations. There are three different components to each "product" in a dictionary list:

1) a letter, e.g. 'A' (first part of the product code, unique to each product entry). Let's say the range here is:

['A', 'B', 'C']

2) a letter and number, e.g. 'S2' (2nd part, has several variations...might be 'D3' or 'E5' instead)

3) a period ('.') and a letter, e.g. '.X' (3rd part, unique to each product entry). Let's say the range here is:

['.X', '.Y', '.Z']

Since the 2nd part listed above has the most variations, my starting assumption is to construct dicts lists with the 1st and 3rd parts together, in order to reduce the number of different listsdicts, since they are uniquely paired, e.g. 'A.Z'...but, I would still need to split each entry then insert the 2nd part, between them, via some 'concatenate' command. So, question is: if I have another dict list with all variations of the 2nd part, what function(s) should I use to construct all variants of a product?

The total combination of examples:

ListOne = ['A', 'B', 'C']
ListTwo = ['D3', 'D4', 'D5', 'E3', 'E4', 'E5']
ListThr = ['.X', '.Y', '.Z']

I need to create new dicts lists as concatenations of all three dicts lists, e.g. 'AD3.X', but there are no variants for ListOne vs ListThr, it will always be 'A' matched to '.X' or 'B' and 'C' matched to '.Y'...ListTwo products concatenated between ListOne and ListThr products will need to be iterated so all possible combinations are output as a new dict list e.g.

ListOneNew = ['AD3.X', 'AD4.X', AD5.X', 'AE3.X', 'AE4.X', 'AE5.X']
ListTwoNew = ['BD3.Y', 'BD4.Y', 'BD5.Y', <and so on...>]

For simplicity's sake, should the script have a merged version of ListOne and ListThr e.g.

List = ['A.X', 'B.X', 'C.Z']

and then split & concatenate with ListTwo products, or just have three Lists and concatenate from there?

3
  • 4
    Those aren't dictionaries, those are lists... Commented May 12, 2017 at 18:43
  • All three? I thought that if both letters and numbers are in a product name, it becomes a dictionary, whereas lists would be products made up only of letters. Commented May 12, 2017 at 18:49
  • 1
    No. Dictionaries and lists are completely different data structures. Commented May 12, 2017 at 18:53

2 Answers 2

1
from itertools import product

result = [a[0] + a[1] + a[2] for a in list(product(DictOne, DictTwo, DictThr))]
Sign up to request clarification or add additional context in comments.

Comments

1

With list comprehension

final = sorted([a+b+c for c in DictThr for b in DictTwo for a in DictOne])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.