# Integration Toolkit Solutions Wiki

**URL:** <https://forum.inductiveautomation.com/t/integration-toolkit-solutions-wiki/105080>\
**Category:** 3rd Party Modules\
**Tags:** ignition81, scripting\
**Created:** [April 14, 2025, 9:02pm UTC](https://forum.inductiveautomation.com/t/integration-toolkit-solutions-wiki/105080 "2025-04-14T21:02:36Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 16, 2025, 10:06pm UTC](https://forum.inductiveautomation.com/t/integration-toolkit-solutions-wiki/105080/8 "2025-04-16T22:06:22Z")

</div>

## Merging lists into a single list of objects where each list is represented by its own key in each object

How would I merge multiple lists into a single list of objects/dicts, where each list is given its own key name?  
e.g.

```python
BobList = [1,2,3,4]
PeterList = ['a','b','c','d']
MergedList = [{'Bob': 1, 'Peter': 'a':}, {'Bob': 2, 'Peter': 'b'} ...] # create this one

```

I want to be able to name the keys explicitly, they won't match the list name exactly

* * *

**Solutions**

Requirements:

- The number of supplied keys is equal to the number of lists to combine
- The lists being combined are the same length
- The keys should all be unique, otherwise the last lists values associated with that key will be used.

### Simple solution by @Transistor

> **Expression binding**
>
> ```python
> forEach(
> asList(1, 2, 3, 4),
> asMap(
> 'Bob', it(),
> 'Peter', asList('a','b','c','d')[idx()] 
> )
> )
> or
> 
> forEach(
> {this.custom.BobList},
> asMap(
> 'Bob', it(),
> 'Peter', {this.custom.PeterList}[idx()] 
> )
> )
> 
> ```

Note: If length of BobList \< length of PeterList the expression will return null.

### Advanced solution by @lrose

Use `transform()` to snapshot a list of lists (Lists to be combined), then use `forEach()` in it's range form to "enumerate" the lists. Finally, use `asMap()` and `forEach()` to loop over the list of keys, and return a list of lists where each nested list is a key, value pair for the map.

> **Expression binding**
>
> ```python
> transform(
> asList(
> asList(1,2,3,4),
> asList('a','b','c','d'),
> ),
> forEach(
> len(value()[0]), //acts like pythons range()
> asMap(
> forEach(
> asList('Bob','Peter'),
> asList(
> it(),value()[idx()][idx(1)]
> )
> )
> )
> )
> )
> 
> ```
> 
> or
> 
> ```python
> transform(
> {path.to.list.of.lists},
> forEach(
> len(value()[0]),
> asMap(
> forEach(
> {path.to.list.of.keys},
> asList(
> it(),value()[idx()][idx(1)]
> )
> )
> )
> )
> )
> 
> ```

> **Advantages**
>
> The main advantage of this approach over the one provided by @Transistor is it allows for any arbitrary number of List/Key combinations, with out further editing of the expression. This means that the expression will work even if the expression parameters are produced dynamically, so long as the requirements for the function to work are met.
> 
> Using `transform()` allows for a single reference to the List of Lists, meaning it can be referenced multiple times throughout the expression with out needing to do the work of dereferencing the property path to get the value. Making the expression more performant, especially as the number of list/key combinations grows.

Some clarification on the inner most `forEach()` loop.

In this expression `it(), value()[idx()][idx(1)]`, `it()` is the current key, `value()` is the list of lists, `[idx()]` is the current index of the key used to retrieve the list associated with that key from the lists of lists, `[idx(1)]` is the value in the associated list at the "enumerated" index.

So the first time through the loop, it would return [['Bob',1],['Peter','a']], which when handed to `asMap()` results in the map `{'Bob':1,'Peter':'a'}`.

> **Functions used**
>
> - [transform()](https://www.automation-pros.com/toolkit/doc/utility.html#transform) Makes a snapshot of the list of lists.
> - [forEach()](https://www.automation-pros.com/toolkit/doc/iteration.html#foreach) loops through the items in the source.
> - [asMap()](https://www.automation-pros.com/toolkit/doc/iteration.html#asmap) produce a Java Map with string keys.
> - [asList()](https://www.automation-pros.com/toolkit/doc/iteration.html#aslist) unconditionally assembles all of its arguments into a List.
> - [len()](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/len) returns the length of its argument.
> - [it()](https://www.automation-pros.com/toolkit/doc/iteration.html#it) Delivers the key from the lists of keys
> - [idx([depth])](https://www.automation-pros.com/toolkit/doc/iteration.html#idx) retrieves the loop index for an index for the forEach iterator based on the value of its depth argument.

---

_[View the full topic](https://forum.inductiveautomation.com/t/integration-toolkit-solutions-wiki/105080)._
