## How to remove lists that are the same after reversed?

This is the place for queries that don't fit in any of the other categories.

### How to remove lists that are the same after reversed?

Hi dear members, i have a python list of lists:
L=[[8, 7, 16, 28], [21, 2, 4, 10], [10, 4, 2, 21], [20, 1, 0, 15], [15, 0, 1, 20], [28, 16, 7, 8]]
I want to obtain: [[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]
how to reverse either of the lists : example: [[21, 2, 4, 10], [10, 4, 2, 21]] in to [[21, 2, 4, 10], [21, 2, 4, 10]]

Finally I can get the unique lists using set().
birda

Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

### Re: How to remove lists that are the same after reversed?

Answered in reverse order:

birda wrote:how to reverse either of the lists : example: [[21, 2, 4, 10], [10, 4, 2, 21]] in to [[21, 2, 4, 10], [21, 2, 4, 10]]

Reversing second list:
Code: Select all
`L[1] = L[1][::-1]`

birda wrote:I want to obtain: [[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]

I think to do this you will have to implement the solution above recursively. L.reverse will not work, because it just reverses your list of lists.

birda wrote:Finally I can get the unique lists using set().

Yes.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: How to remove lists that are the same after reversed?

Use reverse()
Code: Select all
`>>> a = [2,3,4]>>> a.reverse()>>> a[4, 3, 2]`
hrs

Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

### Re: How to remove lists that are the same after reversed?

hrs wrote:Use reverse()
Code: Select all
`>>> a = [2,3,4]>>> a.reverse()>>> a[4, 3, 2]`

But reverse() does not work as desired for his nested lists.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: How to remove lists that are the same after reversed?

This should do it:
Code: Select all
`L=[[8, 7, 16, 28], [21, 2, 4, 10], [10, 4, 2, 21], [20, 1, 0, 15], [15, 0, 1, 20], [28, 16, 7, 8]]new=[]for guy in L:    if guy not in new and guy[::-1] not in new:        new.append(guy)print(new)`
Result:
Code: Select all
`[[8, 7, 16, 28], [21, 2, 4, 10], [20, 1, 0, 15]]`

Could probably find a one liner if you try hard enough but this is pretty clear.

tnknepp wrote:
birda wrote:Finally I can get the unique lists using set().

Yes.
Sets can't be made with mutable data types. They would have to be converted to tuples first.

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: How to remove lists that are the same after reversed?

thanks all
birda

Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Return to General Coding Help

### Who is online

Users browsing this forum: No registered users and 10 guests