Hi,
I’ve a list of one element type and want to convert it to singular value as usual. But when I add [0] to my codem it gives me an following error: IndexError: index out of range: 0. Why? I did it before, but what’s wrong now.


Part of the code:
tp_str = ('L=' + str(float(tp) / 1000) + '0').replace('.', ',') #Type name as string and replace . to ,
fam_symb = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()
#I want to make a singular value from list.
filter_tp = [i for i in fam_symb if Element.Name.GetValue(i) == tp_str]
We would have to see the rest of the code to know, but it could be that your filter has no matches.
If you just want to convert the list to a singleton you can always use a quick code block:
item = list[0];
That’s what I’ve tried first.

Here is my code:
def beam_type(prime_beam_types, prime_beam_ln):
ln_beam = []
for prime_beam_type in prime_beam_types:
if prime_beam_ln > prime_beam_type:
ln_beam.append(prime_beam_type)
if len(ln_beam) != 0:
return max(ln_beam)
# The inputs to this node will be stored as a list in the IN variables.
prime_beam_types = IN[0]
prime_beam_lns = IN[1] if isinstance(IN[1], list) else [IN[1]]
# Place your code below this line
prime_beam_lns = List.Flatten(prime_beam_lns)
strs = []
prime_beam_type_upd = []
for prime_beam_ln in prime_beam_lns:
prime_beam_type_ln = []
ln_length = int(round(float(prime_beam_ln.Length), 0)) #Line to int
is_continue = True
while is_continue:
tp = beam_type(prime_beam_types, ln_length)
if ln_length < min(prime_beam_types):
#prime_beam_type_upd.append('Placed Type: ' + str(tp))
is_continue = False
else:
pt = prime_beam_ln.StartPoint #Define first pt
#Place family instance
tp_str = ('L=' + str(float(tp) / 1000) + '0').replace('.', ',') #Type name as string and replace . to ,
fam_symb = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()
filter_tp = [i for i in fam_symb if Element.Name.GetValue(i) == tp_str]
#Define function to get family type above
ln_length -= tp
prime_beam_type_ln.append(tp)
prime_beam_type_upd.append(prime_beam_type_ln) #Sublist to final list
# Assign your output to the OUT variable.
OUT = filter_tp
Get the 0 index item from the final filter_tp list in the OUT line. You’re currently doing it inside the loop that’s building that list, meaning that many of those items will fail. Out of x number of items being iterated, only 1 has a value being added to your list. All the other items you’re iterating through have no values, so item[0] would fail on them.
I have to use this singular value in the nextfew lones inside this loop. What can i do?
Inside the existing loop? You still have to deal with the items that return empty lists. You could just do a check for if len(filter_tp) > 0: to make sure you’re only continuing with lists that contain values.
Yes, I want to get family type from string and place family instance while looping.