# Working with Dictionaries in Python

**URL:** https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440
**Category:** Revit
**Tags:** python
**Created:** [November 21, 2019, 10:08am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440 "2019-11-21T10:08:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![bialmeida](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bialmeida/32/62015_2.png) [@bialmeida](https://forum.dynamobim.com/u/bialmeida)
#### Post date: [November 21, 2019, 10:08am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/1 "2019-11-21T10:08:39Z")

</div>

Hey everyone,

i created a python node and need some help finding a way of getting what i want, i am not very familiar with dictionaries yet:

I would like python to check if the keys are identical and in this case calculate the areas of the rooms contained in Dictionary 1, copy this value into a Parameter of the fist area (index 0) in Dictionary 2, for example:

if key Dictionary 1 == key Dictionary 2, then calculate area Room 1931747 + Area Room 1931748 + Area Room 1931749. Then pick index 0 of corresponding list in Dictionary 2 (Area 1931821) and copy this value into a Project Parameter called room area. The other Areas contained in the same list should get 0 as value.

I am doing this because i want to create a schedule that Shows Rooms and Areas at the same time in Revit, so i need to get the area of all Rooms into the gross area schedule… Thanks!!

 ![2019-11-21%2011_04_08-Dynamo](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/6/76db3c06b448a22dec324b09bef6610344dcd98f.png)

---

<div class="post-metadata">

### Author: ![Kibar](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kibar/32/80416_2.png) [@Kibar](https://forum.dynamobim.com/u/Kibar)
#### Post date: [November 21, 2019, 9:26pm UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/2 "2019-11-21T21:26:49Z")

</div>

I guess there might be an easier way to reach your goal (using less python), but this seems like a great python learning task 🙂  
It might cause confusion because you´ll have to translate between Python dictionaries and .NET dictionaries.

To break down what you´ll need to know:

- [Dictionaries in Python and how to loop through them.](https://realpython.com/iterate-through-dictionary-python/)
- [Convert between units (RevitAPI)](https://www.revitapidocs.com/2020/9cc2c0ea-f59f-9d76-ce19-ae7eede03bbd.htm)
- get/set parameter value in python (search this forum)
- [Create/Output Dynamo dictionaries](https://digitteck.com/dotnet/dynamobimdotnet/dictionaries-from-python-to-graph/) (in case you want to output a new dictionary)

* * *

Take apart your dictionaries before feeding it into the python script (to keep it easier):

 ![dyn-py-dict](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/6/365a1e871cdc407d546be8f9423947c1fa214ce5.png)

Try to break down your tasks into small pieces and go from there.  
Everything that I have commented out has been answered in one way or another on this forum.

```
	# Recreate python dictinairy
	room_keys = IN[0]
	room_values = UnwrapElement(IN[1])
	room_dict = dict()
	for key, value in zip(room_keys,room_values):
		room_dict[key]= value
	
	
	# Recreate python dictionairy
	area_keys = IN[2]
	area_values = UnwrapElement(IN[3])
	area_dict = dict()
	for key, value in zip(area_keys, area_values):
		area_dict[key]= value
	
	
	for level in room_dict.keys():
		if level in area_dict.keys():
			# Get all rooms of that level (rooms_of_level)
		
			# Create total value 'total = 0.0'
	
			# for room in rooms_of_level:
			#	-> Get room area (sqf)
			#	-> total += room area
	
			# Convert internal units of total area
			# sqf to m2 (RevitAPI)
			
			# Get Areas on that level
			# for index, area in enumerate(Areas):
			#	-> Get parameter 'room area' of area
			# -> if index == 0:
			# -> Set parameter 'room area' = total_sqm
			#	-> else:
			# -> Set parameter 'room area' = 0.0
```

---

<div class="post-metadata">

### Author: ![cgartland](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cgartland/32/45378_2.png) [@cgartland](https://forum.dynamobim.com/u/cgartland)
#### Post date: [November 21, 2019, 11:14pm UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/3 "2019-11-21T23:14:53Z")

</div>

This can also be simplified slightly by directly constructing a dictionary with the tuples output by the zip function:  
`room_dict = dict(zip(room_keys, room_values))`

Also, .NET dictionaries are automatically converted in Dynamo Core 2.1 and newer, but I don’t think this has been implemented in the Dynamo Revit versions yet.

---

<div class="post-metadata">

### Author: ![bialmeida](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bialmeida/32/62015_2.png) [@bialmeida](https://forum.dynamobim.com/u/bialmeida)
#### Post date: [November 25, 2019, 8:58am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/4 "2019-11-25T08:58:19Z")

</div>

Thank you very much, this is a gret challenge for me!  
I would have a first question if you dont mind:

python dictionaries work with pairs, right (key + value)?  
I can’t understand how a dictionary can be created once i provide a different number of keys (levels?) and values (areas?)…How does the Dictionary look like in the end? How are the pairs created?

Thanks!

---

<div class="post-metadata">

### Author: ![Kibar](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kibar/32/80416_2.png) [@Kibar](https://forum.dynamobim.com/u/Kibar)
#### Post date: [November 25, 2019, 9:16am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/5 "2019-11-25T09:16:07Z")

</div>

{ key : value }  
In this case your key is the level name and its value a list of areas.  
{ string : list }  
{ “Obergeschoss” : [area1, area2]}  
You can add new elements to this list.

---

<div class="post-metadata">

### Author: ![neel-studio4](https://avatars.discourse-cdn.com/v4/letter/n/a88e57/32.png) [@neel-studio4](https://forum.dynamobim.com/u/neel-studio4)
#### Post date: [January 12, 2021, 7:13am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/6 "2021-01-12T07:13:47Z")

</div>

just as an added help ,  
to write or edit dictionaries in dynamo python u need to add the following imports

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/7/778f1ff29cf61913609e76d78bdbe42ae34dd95b.png)

```
> import sys
> import clr
> import System.Collections.Generic
> Dictionary = System.Collections.Generic.Dictionary[System.String,System.Object]()
> 
> # The inputs to this node will be stored as a list in the IN variables.
> dict = IN[0]
> 
> # Place your code below this line
> 
> # Assign your output to the OUT variable.
> OUT = dict
```

---

<div class="post-metadata">

### Author: ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)
#### Post date: [January 12, 2021, 11:44am UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/7 "2021-01-12T11:44:46Z")

</div>

Hello,  
since Dynamo 2.3 the Dynamo wrapper convert automatically a Python dictionary to a Net dictionary

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/c/fcf3dab075f70d8b1099d9fc90155e62c9224096.png)

Before this version (like version 2.0.4) it is advisable to convert a Python dictionary to Net only if the dictionary is assigned to the variable OUT (and the key must be a string).  
Inside the node we can write and edit a Python dictionary without problems

---

<div class="post-metadata">

### Author: ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)
#### Post date: [November 22, 2022, 4:00pm UTC](https://forum.dynamobim.com/t/working-with-dictionaries-in-python/43440/8 "2022-11-22T16:00:09Z")

</div>

A post was split to a new topic: [Python Dictonnary with Sheets List](https://forum.dynamobim.com/t/python-dictonnary-with-sheets-list/83380)
