Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list element by position

I want to give a number and return the element of this position. List lab = (R K K K K) and I want to know if something like this (position 1 lab) exists on lisp. Like in C return lab[1].

like image 506
Manolis Pap Avatar asked Mar 21 '16 10:03

Manolis Pap


People also ask

How do you get the position of an element in a list?

To find a position of the particular element you can use the index() method of List class with the element passed as an argument. An index() function returns an integer (position) of the first match of the specified element in the List.

How do I find a specific element in a list Python?

To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.

How do you find the position of an element in a list in Java?

The indexOf(Object) method of the java. util. ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.

How to find the position of an element in a list?

In this Python tutorial, we will learn how to find the position of an element in a list in Python. To find the position of an element in a list, we need to understand the concept of the index of the elements in a list. In a list, there is a term called “ index “, which denotes the position of an element.

How do I find the desired elements of a list?

The first approach to find the desired elements is to use list comprehension. We traverse through ‘lst2’ and for each i th element, we output lst1 [i].

How to retrieve value from list by position in Microsoft Excel?

To retrieve value from list by position, we will use INDEX function to get the output. What is Index function and how we can use the Index function in Microsoft Excel? INDEX: Returns a value or reference of the cell at the intersection of a particular row and column, in a given range.

How to find a list item at an index in SQL Server?

Starting from SQL Server 2012, you can use the T-SQL CHOOSE () function to find a list item at a specified index position within a list. The syntax goes like this: CHOOSE (index, val_1, val_2 val_n ]) Where index is an integer that represents the position within the list that you want to return.


2 Answers

In Common Lisp the operator that gets the n-th element of a list is called nth (see the manual):

(nth 2 '(a b c d))  ; returns C

A related operator is nthcdr that returns the rest of the list starting from the n-th element:

(nthcdr 2 '(a b c d)) ; returns (C D)
like image 122
Renzo Avatar answered Nov 12 '22 16:11

Renzo


For an operator that works on vectors and proper lists, see elt.

(let ((list (list 'a 'b 'c 'd)))
       (prog1 list
         (setf (elt list 1) 1)))
=> (A 1 C D)
like image 20
coredump Avatar answered Nov 12 '22 15:11

coredump