Getting index of sublist within a list using an element in the sublist

I would like to insert a sublist within the list (ActivityDB). But I have a problem finding the index of the sublist within the list, using the word “Hello” (ActivityName) that is inside the sublist. How can I do that?
And I would like to replace existing sublists of the same “ActivityName” with updated values. If no such sublist is available, I want to add a new sublist with that ActivityName.

If you see the screenshot, the problem I am having is, even though the ActivityName is detected, it is not able to return the index of the sublist. Which is understandable because it is a sublist not a string. How do I present the relevant sublist as a “thing” from an element (ActivityName) inside the sublist, such that the index of the sublist in the list can be reutrned correctly?

Please help.

If I’m not mistaken, it doesn’t seems that you use the “ActivityName” variable. You are only using the “global ActivityName” variable under the block that says “For each ActivityName in list”

I think you are right. That must be confusing. So, I changed the “ActivityName” for the “for each” block to simply “Activity”. My problem is not yet resolved though. Should I do something with the “Activity”? Do you have any suggestions on what I should do to achieve what I want?

If you look at the picture, there’s a problem with what I put in as a “thing”. What do you think? I want to get the index of the sublist within the list, using an element in the sublist. Any suggestion?

Under "for each Activity in list ActivityDB" do it like that: if contains text=get Activity, piece="what you are looking for"
Then: replace list item list =get global ActivityDB,
index = index in list thing = get Activity, list= get global ActivityDB
Replacement: your new value

1 Like

Thank you. It is closer to what I wanted! Now it is able to update the old values in the sublist to the new values. However, everytime I press the button, it does not only update the old values but also adds another sublist to the database, that’s exactly the same (with the new values). Do you know why?

So every button click, does not only “replace” but also “add items” at the same time. If I press the second button, the more times I press, the more items it adds in one click.

Add a “break” block instead of add item to list. That will stop the loop when it find what to replace

blocks (17)

I did that. I also tried not using the break block. Now, it doesn’t add sublists, which is good. And it also replaces the old value, which is good. But it won’t register any button clicks other than the very first one I pressed. Every time the first button is pressed, it will update the values. But other buttons does nothing. Hmmm…

Did you update the "get id" value when you click on button ? Because it won’t find the previous value in the list if it has been modified.

I believe every time I click the button, the getID value always updates automatically. You can see the top part of the block in the picture. Thing is, after I press the first button, any subsequent buttons does nothing. But previously, before I got rid of the “add items” block, other buttons work, but they add unwanted items.

Sorry, I am really a newbie and this is the first day I am trying to put stuff in TinyDB :pensive:

Give me a minute, I think I have an Idea.

Gladly… Thanks so much for your help.

Is it possible that the first button registers because the list was first empty, so it gets to store a sublist in ActivityDB, which happens after the “for each”, then it can compare with what’s in the ActivityDB and replace with updated values on subsequent same button clicks. But with other button presses, they get stuck in the “else” and don’t get to store any values to TinyDB? Can’t think of a solution though. It seems like “add items” should not be put in a “for each” in my case. What can I do?

Add that to the else section. in theory it should work as I am not able to test is but what is should do is: It checks if there is a sub-list with current value then replace it otherwise add it to the list.


WOW!! It works! Thank you so much for your help and effort!! I will internalize what you did and learn. :+1: :grin:

1 Like

I was happy to help :+1:t5:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.