Avoid warning while using GetItemAtIndex

Hello,

I want to use GetItemAtIndex for one list, but I need to keep the order of the list, although the first four values can not be find in the list. I just want to have an empty or blank value, instead of null. Everything in my script works like this, I just want to get rid of the warnings. Is there any way I can avoid the warnings when using GetItemAtIndex which can not be found in the list?
Thanks!

Hi Klara, in this case you could use a bool mask to remove anything that equals “”

I would be interest to see how you arrive at this point since I typically use GetItemAtIndex paired with List.AllIndicesOf, this doesnt return any blank values. Example below which may and probably not useful to your workflow

Hi @klara.steinMZ7FJ ,

For this I always use a Python loop containing an if-statement that checks if the value is -1, if so it then appends an empty list. If that is not the case it gets the index of that item and append it.

I also tried to do this with DesignScript but that only returns a single empty list instead of multiple.

You just need to apply a condition to only get items with a valid index.

Keep in mind however that you’ll have to continue this condition for all invalid inputs.

2 Likes

Thanks for the answers! I tried your solution, but unfortunately I still get the warning

Thank you, but I need to keep the empty values, because I need to keep the list in this order with the blanks, so this is not working for me.

See if something like this works:

my_list = IN[0]
my_indices = IN[1]

def get_sublist(lst, indices):
  sublist = []
  for index in indices:
    if index == -1:
      sublist.append([])
    else:
      sublist.append(lst[index])
  return sublist

OUT = get_sublist(my_list,my_indices)

I just copied your code and got this error. I don’t know anything about Python so I don’t know if its not working or if this is another error :smiley:

Strange, I’ll take a look later today when I have a little bit more time.

You don’t have matching list lengths. You’ll have to deal with this first. Based on the error you’re getting with python, you may have to convert your indices to integers as well.

Yeah of course I don’t have matching lengths, but I need to keep the order, that’s why I want to work with empty values. That’s like the whole problem

That’s fine. I’m just saying that’s may be why the If statement won’t work for you. If you require different list lengths then you can always use another solution like python. In which case you need to address the error it’s giving you.