# Conversion from an array of python dictionaries to an array of arrays using transform

**URL:** <https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 27, 2023, 5:54pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600 "2023-06-27T17:54:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [June 27, 2023, 5:54pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/1 "2023-06-27T17:54:12Z")

</div>

Hi all,

I'm currently working on trying to take a return type of an array of python dictionaries to and array of arrays using transforms. However, I have thus far been unsuccessful. Does anyone know a way to do this using any of the base 4 transforms?

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/2/f2db0e69d86f438f0d51f31954da0cead6965969.png)

Thank you,

Cam

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 27, 2023, 7:11pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/2 "2023-06-27T19:11:02Z")

</div>

Can you give an example of your input data and how you would like it to look after the transform? If the keys aren't the same in each dictionary I suspect that you will be in trouble.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [June 27, 2023, 8:06pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/3 "2023-06-27T20:06:54Z")

</div>

If I'm understanding you correctly, you can just loop through the dictionaries in the array, and then you can loop through the keys in each dictionary. Something like this might work:

```auto
def convertData(data):	
	return [[dic[key] for key in dic] for dic in data]

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 27, 2023, 8:12pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/4 "2023-06-27T20:12:26Z")

</div>

> [@djs](#):
>
> `return [[dic[key] for key in dic] for dic in data]`

That part can be shortened to just:

```auto
	return [d.values() for d in data]

```

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [June 27, 2023, 8:17pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/6 "2023-06-27T20:17:32Z")

</div>

You gentlemen are ingenious.

Thank you

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 27, 2023, 8:20pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/7 "2023-06-27T20:20:53Z")

</div>

Do be aware that key order in jython dictionaries is unspecified, and may not match your expectations. If you need keys pulled in a specific order, you need smarter code.

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [June 27, 2023, 8:23pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/8 "2023-06-27T20:23:28Z")

</div>

Fair enough. How would I go about editing it so it returns it in order then?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 27, 2023, 8:48pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/9 "2023-06-27T20:48:46Z")

</div>

You need a list of the keys you want in the order you want them. Then use Daniel's construct but using the desired keys in the inner loop instead of the keys in the order the dict() supplies.

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [June 27, 2023, 8:54pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/10 "2023-06-27T20:54:30Z")

</div>

Okay, that would make sense. Just for my own understanding @djs solution is more like an enumeration type where the 'key' acts as the index and 'dic' acts as the value type, correct?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 27, 2023, 9:25pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/11 "2023-06-27T21:25:02Z")

</div>

`dic` is short for `dict`, but using `dict` as a variable name breaks things (it is the name of a built-in in python).

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [June 27, 2023, 9:32pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/12 "2023-06-27T21:32:14Z")

</div>

Okay, that would make sense. I appreciate your help and explanation 😄

---

<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:** [June 28, 2023, 12:50pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/13 "2023-06-28T12:50:35Z")

</div>

```auto
[[dic[key] for key in dic] for dic in data]

```

is practically equivalent to

```auto
l=[] 
for dic in data:
   ll=[] 
   for key in dic:
      ll.append(dic[key]) 
   l.append(ll)

```

A list comprehension will run slightly faster though

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 28, 2023, 2:01pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/14 "2023-06-28T14:01:25Z")

</div>

If you want it to retain order the easy jython way is to use an ordered dict.

```auto
from collections import OrderedDict

d = OrderedDict()
d['someKey'] = 'someValue'
# etc

```

and then iterating though this dictionary with `for k, v in d.items()` will run through them in the order that you added the key-value pairs.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 28, 2023, 2:20pm UTC](https://forum.inductiveautomation.com/t/conversion-from-an-array-of-python-dictionaries-to-an-array-of-arrays-using-transform/75600/15 "2023-06-28T14:20:39Z")

</div>

> [@bkarabinchak.psi](#):
>
> retain order the easy jython way is to use an ordered dict

That won't survive assignment to a Perspective property. It will come back out unordered. If ordered keys are critical and not known ahead of time, use a single-row dataset instead of a map.

(My Simulation Aids V2 `columnsOf()` function returns an ordered Java map that holds its ordering within the expression using it, but loses the order when placed in a Perspective property. Perspective's property wrappers are an ungodly mess.)
