See the top rated post in this thread. Click here

# Thread: Lists within lists - simplify

1. Login to Give a bone

## Lists within lists - simplify

Is there any more efficant way to convert list of type :

((1 2 3) (2 3 4) ((5 7 3)) (((G))) ((((A)))))
to list
(1 2 3 2 3 4 5 7 3 G A)

I've managed to do this only to this level
((1 2 3) (2 3 4) ((5 7 3)))
but I want for all levels of brackets...

Here is my code :
Code:
```(defun lstnlst1 (lst / lstn)
(foreach el lst
(if (= (listp el) nil)
(setq lstn (cons el lstn))
(progn
(foreach ee el
(if (= (listp ee) nil)
(setq lstn (cons ee lstn))
(progn
(foreach e ee
(if (= (listp e) nil)
(setq lstn (cons e lstn))
)))
)))
))
(setq lstn (reverse lstn))
)```
M.R.

2. Login to Give a bone

## Re: Lists within lists - simplify

try this

Code:
```(defun SimplifyList (lst / looplst a b c y newlst )
(defun looplst (l)
(if (listp (setq y (car l)))
(looplst  y) l)
)
(while (setq a (car lst))
(setq b (cdr lst))
(setq c (looplst a))
(foreach x c
(setq newlst (cons x newlst)))
(setq lst b)
)
(reverse newlst)
)```
Code:
`(setq yourlist '((1 2 3) (2 3 4) ((5 7 3)) (((G))) ((((A))))))`
(simplifylist yourlist)
(1 2 3 2 3 4 5 7 3 G A)

3. Login to Give a bone

## Re: Lists within lists - simplify

You didn't understand question... I want simplification of all possible combinations of lists with many sublists with many brackets...

Command: (setq lst '(((( 1 3 6) (((((6 9 4))))) ))) )
((((1 3 6) (((((6 9 4))))))))

Command: (simplifylist lst)
(1 3 6)
As you can see I get only first part of this new list type...

Any ideas...

M.R.

4. Login to Give a bone

## Re: Lists within lists - simplify

Funny how you post a example of your list then give another format afterwards

But it does work on the first list condition right?

Now, a long time ago i;'ve was playing around with recursion for nested list such as yours, I noticed that
there are too many variations,

So i came up with just one routine no matter how nested the list is

Code:
```(defun ConvertList  (lst)
(vl-list->string
(vl-remove-if
'(lambda (x)
(or
(= x 40)
(= x 41)))
(vl-string->list
(vl-princ-to-string lst))))
")")
)
)```

Hope this one helps,

5. Login to Give a bone

## Re: Lists within lists - simplify

That's it, you did it...

Thanks, pbejse
(nice idea to convert list to string and then remove all brackets with vl-remove-if and lambda)
This is real programming solution, I would give you an accolade if I could...
Thanks again...

6. Login to Give a bone

## Re: Lists within lists - simplify

Glad it works for you M.R.

Cheers

7. Login to Give a bone

## Re: Lists within lists - simplify

Brilliant.

8. Login to Give a bone

## Re: Lists within lists - simplify

Code:
```(defun AT:ListCondense (lst)
;; Condense list of sublists to a single list
;; lst - list of lists to process
;; Alan J. Thompson, 08.10.10
(if lst
(if (listp lst)
(append (AT:ListCondense (car lst)) (AT:ListCondense (cdr lst)))
(list lst)
)
)
)```

9. Login to Give a bone

## Re: Lists within lists - simplify

NIce one Alan.

Are you using the function within itself here???
If you have the time, could you explain what you are doing?

Cheers

10. Login to Give a bone

## Re: Lists within lists - simplify

Originally Posted by jmcshane
NIce one Alan.

Are you using the function within itself here???
If you have the time, could you explain what you are doing?

Cheers
Yes, it's recursive.
When I get a chance, I'll talk you through it.

Page 1 of 3 123 Last

#### Posting Permissions

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