remove-item command

14 views
Skip to first unread message

Sarah Smet

unread,
Jun 16, 2022, 10:27:27 AM6/16/22
to netlogo-users
Hello,

I can't succeed in using the `remove-item` command inside lists of list.
For example
set my-list [ ["cereal" "cereal" 0.8] ["cereal" "fruit" 0.002] ["cereal" "forage" 0.06] ]

how can I remove the item 2 of the second list (value 0.002) ?

I tried to use `remove-item` command the same way I use replace-item (which works) but it doesn't work :
set historic-data replace-item 1 historic-data (replace-item 2 item 1 historic-data ("apple"))

Thanks, Sarah

Pradeesh Kumar K V

unread,
Jun 16, 2022, 12:15:29 PM6/16/22
to netlogo-users
Hello Sarah,

Your code works. I tried the code: set my-list replace-item 1 my-list (remove-item 2 item 1 my-list) 

and it gives the output: [["cereal" "cereal" 0.8] ["cereal" "fruit"] ["cereal" "forage" 0.06]]

The only difference with the replace-item code is that you don't have to give an input (such as "apple").

Best,

Pradeesh

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/59e4f37e-bd3c-4775-8557-59925dc0da71n%40googlegroups.com.

James Steiner

unread,
Jun 16, 2022, 1:01:49 PM6/16/22
to netlogo-users
Hi! 

You don’t show how you are using remove item, so I’m note sure where the error is. I think maybe you’ve got replace and remove switched.  Could you please say exactly what you mean by “it doesn’t work?”

Do you really want to remove the value, or just replace it with a blank value?

It helps in these situations to avoid excess brevity… break it all down :

Let old-row item 1 historic-data
Let new-row remove-item 2 old-row
Set historic-data replace-item 1 historic-data new-row

Then  put it back together:

Set historic-data replace-item 1 historic-data (remove-item 2 (item 1 historic-data)

I am not a fan of working with lists of lists, especially if they have to change. 

It’s sometimes useful to use a list or set of “data-row” turtles. These turtles own variables that are the columns of your table. 

So to get the row you use 

Let row item 2 historic-data

Then replace a value

Ask row [ set fruit “apple” ]

Removing a column isn’t really a thing, but you can make it blank (“”) or 0 or false or even use “nobody” as a stand-in for “null”

And you can query your table.

IF “history” is an agentset of data-row turtles, an SQL-style like 

SELECT TOP 1 FRUIT FROM HISTORY WHERE TIMESTEP = ‘1234’

Becomes 

[ fruit ] of ( one-of ( history with [ timestep = “1234” ] ) )

Somewhere in the deep history of this list or it’s predecessor is a long discussion of this technique. 

~~James

On Thu, Jun 16, 2022 at 10:27 AM Sarah Smet <sarah...@gmail.com> wrote:
Hello,

I can't succeed in using the `remove-item` command inside lists of list.
For example
set my-list [ ["cereal" "cereal" 0.8] ["cereal" "fruit" 0.002] ["cereal" "forage" 0.06] ]
   3? 
how can I remove the item 2 of the second list (value 0.002) ?

I tried to use `remove-item` command the same way I use replace-item (which works) but it doesn't work :
set historic-data replace-item 1 historic-data (replace-item 2 item 1 historic-data ("apple"))

Thanks, Sarah

Sarah Smet

unread,
Jun 20, 2022, 1:41:23 PM6/20/22
to netlogo-users
Hi James and Pradeesh,
Thanks for your answers.
1) indeed, using replace-item and remove-item works... I had not tried that combination because it did not make sense to me... but knowing that it actually works that way is useful.
2) from James discussion point, I changed the way I implemented the code to not have lists of lists that must be altered, have items removed.

Thanks ! Best
Sarah

Sarah Smet

unread,
Jun 20, 2022, 1:41:29 PM6/20/22
to netlogo-users
Hi Pradeesh,

Indeed, when using both replace-item and remove-item it does work. I had not tried that combination actually...!
Thanks for your reply !
Best, Sarah
Reply all
Reply to author
Forward
0 new messages