# Thread: Break up a list of numbers into groups of 2

1. ## Break up a list of numbers into groups of 2

Registered forum members do not see this ad.

My job took me away from AutoCAD for a good while but I'm back behind a desk and I'm really rusty with my AutoLISP.

How can I take a list of numbers and put them of groups of two? For example, if my list is this:

Code:
`(1 2 3 4 5 6 7 8)`
How can I get it to look like this:

Code:
`(1 2)(3 4)(5 6)(7 8)`
or even this:

Code:
`(1 . 2)(3 . 4)(5 . 6)(7 . 8)`
As always, any help would be appreciated.

2. Code:
`(defun f ( l ) (if l (cons (list (car l) (cadr l)) (f (cddr l)))))`
Code:
`(defun g ( l ) (if l (cons (cons (car l) (cadr l)) (g (cddr l)))))`
Generic function.

3. I guess I should've known you would already have a solution. Thanks!

4. Or maybe:

Code:
```(defun test (l / tmp)
(and (zerop (rem (length l) 2)
(while (< (length l) 1)
(setq tmp (cons (list (car l) (cadr l)) tmp)
(setq l (cddr l))
tmp))```
-David

5. Registered forum members do not see this ad.

Originally Posted by David Bethel
Or maybe:

Code:
`(defun test (l / tmp)....`
-David

something missing David? .. perhaps
Code:
```(defun test (l / tmp)
(and (zerop (rem (length l) 2))
(while (> (length l) 1)
(setq tmp (cons (list (car l) (cadr l)) tmp)
l (cddr l))
))
(reverse tmp)
)```
Code:
```(defun _relist  (l n / x ln ls)
(if (zerop (rem (length l) n))
(repeat (/ (setq ln (length l)) n)
((lambda (j)
(repeat j
(setq x (cons (nth (setq ln (1- ln))  l)
x) )))
n)
(setq ls (cons x ls)
x  nil)
))
ls)```
(_relist '(1 2 3 4 5 6 7 8 9 10) 1)
((1) (2) (3) (4) (5) (6) (7) (8 ) (9) (10))
(_relist '(1 2 3 4 5 6 7 8 9 10) 2)
((1 2) (3 4) (5 6) (7 8 ) (9 10))
(_relist '(1 2 3 4 5 6 7 8 9 10) 3)
nil
(_relist '(1 2 3 4 5 6 7 8 9 10) 4)
nil
(_relist '(1 2 3 4 5 6 7 8 9 10) 5)
((1 2 3 4 5) (6 7 8 9 10))

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts