# Better Bruteforce Get Parameter?

**URL:** <https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226>\
**Category:** Developers\
**Tags:** revit, bakery, python\
**Created:** [August 27, 2023, 7:09am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226 "2023-08-27T07:09:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 27, 2023, 7:09am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/1 "2023-08-27T07:09:25Z")

</div>

What do you think of this for getting parameters that cannot be gotten with `Element.GetParameterValueByName` ? Is there a more efficient method?

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

```auto
def get_param(data, prefix):
  prefix = prefix + " : "
  results = []
  for lst in data:
    for item in lst:
      if item.startswith(prefix):
        param = item[len(prefix):]
        results.append(param)
  return results
    
param = get_param(IN[0], IN[1])    
OUT = param

```

Bakery’s `Bruteforce Get Parameter via String Split V2` seems heavy, especially for big datasets.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/a/5ae5914812d5fcc0c1080531e6b96971ffacddf7.png)

Thanks to [Claude 2](https://claude.ai/) for the code coaching! It is fantastic to succinctly work out one’s logic and write Python.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/6/66d2ce706424024a57abb3440a15a0d5e285ffd7.png)

It will write summaries for documenting one’s code, too.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/8/9861e9048260384ffd6070fa9045e3c9aa6db66c.png)

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [August 27, 2023, 11:37pm UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/2 "2023-08-27T23:37:46Z")

</div>

You could do this in one line as a list comprehension

```python
OUT = [j for i, j in (param.split(" : ") for param in IN[0]) if i == IN[1]]

```

or a dictionary comprehension

```python
param_dict = {k: v for k, v in (param.split(" : ") for param in IN[0])}
OUT = param_dict.get(IN[1])

```

---

<div class="post-metadata">

**Author:** ![jmark](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jmark](https://forum.dynamobim.com/u/jmark)\
**Post date:** [August 28, 2023, 2:31am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/3 "2023-08-28T02:31:02Z")

</div>

just adding a new idea on how to get this by splitting the string with .split() and the putting it to a new list

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

the script is asking if one part is equal to the given string which is IN[1] then out the corresponding part as a result.

```auto
data = IN[0]

result = []
other_value = []

for item in data:
    parts = item.split(":")
    if parts[0].strip() == IN[1]:
        n = parts[1]
        result.append(n)
    else:
        other_value.append(parts[1])

OUT = result

```

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 28, 2023, 7:26am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/4 "2023-08-28T07:26:39Z")

</div>

> [@jmark](#):
>
> `parts = item.split(":")`

I refactored it to make sure there is no leading space and to run in Python 2 (Revit 2022.1)

```auto
data = IN[0]
result = []
for outer in data:
  for string in outer:
    parts = string.split(" : ")
    if parts[0] == IN[1]:
      result.append(parts[1])
    else:
      result.append(None)  
OUT = result

```

Is that any more efficient? I suppose I can use Tuneup to compare the different methods. Thanks also to @Mike.Buttery for the _comprehension_.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/0/40a39e75ecdae62ed7e295d09487347f4e6918de.png)

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 28, 2023, 7:46am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/5 "2023-08-28T07:46:19Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/b/eb688a2cd0595ade2a183b6ef1c41c39e79861d4.png)  
List comprehension is a little faster, according to Tuneup.

```auto
data = IN[0]
result = [parts[1] if parts[0] == IN[1] else None  
           for outer in data 
           for string in outer  
           for parts in [string.split(" : ")]]
OUT = result

```

versus

```auto
data = IN[0]
result = []
for outer in data:
  for string in outer:
    parts = string.split(" : ")
    if parts[0] == IN[1]:
      result.append(parts[1])
    else:
      result.append(None)  
OUT = result

```

Edit: those Python functions are WRONG (do not use).

---

<div class="post-metadata">

**Author:** ![jmark](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jmark](https://forum.dynamobim.com/u/jmark)\
**Post date:** [August 28, 2023, 7:47am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/6 "2023-08-28T07:47:37Z")

</div>

if you’re worried the white space before and after the segregated strings, on my original code the .strip() method will take care of that.  
`if parts[0].strip() == IN[1]:`  
you can read more about in here: [https://www.w3schools.com/python/ref\_string\_strip.asp](https://www.w3schools.com/python/ref_string_strip.asp)

anyway, there’s a lot of way you can achieve what you were trying to do, this is just one way to do that. I think your original script works as well as the list comprehension by Mr. Mike.

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 28, 2023, 8:25am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/7 "2023-08-28T08:25:21Z")

</div>

I didn’t have the logic correct if the parameter doesn’t exist in the element data.

I asked [Claude 2](https://claude.ai/) in my conversation with it:  
_in[0] is a list with many sublists of varying lengths._

_OUT needs to have the same length as in[0]_

_each element of OUT shall be the result if there is a match, or null_

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/1/41d196dda1cdb8ddfe8fca3b37708111fd131911.png)

Resulting code seems to work (even though I could never come up with it – thanks, AI):

```auto
OUT = [None] * len(IN[0])

for i, outer in enumerate(IN[0]):
  for string in outer: 
    parts = string.split(" : ")
    if parts[0] == IN[1]:
      OUT[i] = parts[1]
      break

```

We can see here that `Stairs` has no `Area` parameter so should return `null`. Quicker, too, because it has `break`.

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

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 28, 2023, 8:59am UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/8 "2023-08-28T08:59:38Z")

</div>

> [@jmark](#):
>
> if you’re worried the white space before and after the segregated strings, on my original code the .strip() method will take care of that.  
> `if parts[0].strip() == IN[1]:`  
> you can read more about in here: [Python String strip() Method](https://www.w3schools.com/python/ref_string_strip.asp)

`.strip()` is a useful function, and I am glad to learn about it 🤓. If we are making a generic node, we want to preserve the original data, which may contain leading and trailing spaces. Let’s hope the data don’t have an extra " : "!

Gotta work on that…

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

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [August 28, 2023, 10:30pm UTC](https://forum.dynamobim.com/t/better-bruteforce-get-parameter/93226/9 "2023-08-28T22:30:04Z")

</div>

> [@truevis](#):
>
> Let’s hope the data don’t have an extra " : "

**Solved?**

```auto
OUT = [None] * len(IN[0])

for i, outer in enumerate(IN[0]):
  for string in outer: 
    parts = string.split(" : ")
    if parts[0] == IN[1]:
      OUT[i] = " : ".join(parts[1:])
      break

```

The key points:

- Initialize OUT with Nones
- Split string on " : "
- If prefix matches, join remaining parts
- Break inner loop to move to next outer item

This cleanly handles:

- Initializing OUT with correct length
- Splitting once on " : "
- Getting full remainder string if matched
- Setting OUT to result or None

By breaking after setting OUT, it avoids unnecessary further processing.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/3/33d766a63f64cc84a674d02bf08fec3d16a9a6e4.png)  
According to Tuneup, the two nodes plus this Python took less than 3 seconds versus around 60 seconds for `Bruteforce Get Parameter Value by Name`  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/9/7992769f6a360c0b594e4b8fa7868a4fe75febc5.jpeg)  
I have used the `Bruteforce` nodes for their convenience but will use this method in the future.
