Working with Dictionaries in Python

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!!

I guess there might be an easier way to reach your goal (using less python), but this seems like a great python learning task :slight_smile:
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:

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

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

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.

1 Like

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?


{ 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.

1 Like

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

> 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

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

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

1 Like