How to delete from list

mrahikka shared this question 10 years ago
Answered

Terve.

What is the easiest way to delete n:th element from a list?


Mikko

Comments (12)

photo
1

HI

If you list1 have n elements

Take[liste1, 1, n] give you list1 minus the n:th element

Daniel

photo
1

Thank you :o)


M

photo
1

... exept it does not work.


I want to delete n:th object from the list, not everything after that.


M

photo
1

Hi

Ok

your liste1 is the first list in which you want to remove the n:th element

make a slider n

liste2=Take [liste1,n,n]

liste3=liste1\liste2

f3fce63bc634f45ec5100ed864e7494d

Files: list.jpg
photo
1

Is there any easier way?


If you want to delete 4th in the list


list1 = {1, 2, 3, 4, 5, 6, 7, 8}


Union[Take[list1, 1, 3], Take[list1, 5, Length[list1]]]


list2 = {1, 2, 3, 5, 6, 7, 8}


Michael, make a command for that.


Mikko

photo
1

Hi

your liste1 is the first list in which you want to remove the n:th element

make a slider n

liste2=Take [liste1,n,n]

liste3=liste1\liste2


More quickly

list=list1 \ Take[list1, n, n]


https://ggbm.at/552197

photo
1

Actually this is a nice example that our students should learn some Set Theory in their maths education.


Mikko

photo
1

there is already an Append[] command for Lists, why not make a Remove[] command for them too?


Furthermore, how do we remove the last element of a list without using another list?


What I do NOT want is:

  • list1 = {1,2,3,4,5}

    list2 = list1\Last[list1]


What I want is:

  • list1 = list1 \ Last[list1]


but GeoGebra won't allow it because of circular definition, any thoughts :question:

photo
2

Hi,


you can keep

list1Helper={1,2,3,4}

list1=RemoveUndefined[list1]

and then remove the last element from list1 using

SetValue[list1Helper,Length[list1],?]

and to append one element to list1

SetValue[list1Helper,Length[list1]+1,newElement]


Notes:

* Append command creates new list, unlike SetValue.

* SetValue lets you make the list one element longer.

* SetValue only works on free lists, therefore you have to use it (in the example above) on list1Helper and not list1 directly.


Cheers,

Zbynek

photo
1

What if I want to push a number into a list, then pull it off, and then push another number into it, and then pull it off again(in scripts, of course, just like what I did in a ggb file about the tower of hanoi)?

photo
1

Similar to kondr's method:


list1 = {4, 6, 7, 3, 2, 5, 7}

list2 = {1, 1, 1, 1, 1, ?, 1}

RemoveUndefined[list1 * list2]


You should be able to make a nice Tool from that :)

photo
1

attached file is completed based on murkle's idea above, enjoy it :)

[Hint: Click on the bases of the towers to move discs]

bf4b295251d4007b1cf7881029a40e8e

https://ggbm.at/552577

© 2022 International GeoGebra Institute