-2

My question is: I have this list:

[['x', '2', 'NOT'], [['sw'], ['y', '1', 'P12'], ['sw']], [['sw'],
['y', '2', 'NOT'], ['sw']], ['x', 'P01'], ['y', 'Xt']]

I'd like to delete the elements duplicate when they appear side by side. Example:['sw']], [['sw']

The end result should be:

 [['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['y', '2', 'NOT'], ['sw'], 
 ['x', 'P01'], ['y', 'Xt']]

I have tried a lot, but I can do that.

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
  • The first step is to make the nesting of your lists uniform. The answers to [this recent question](https://stackoverflow.com/questions/45502776/partial-list-flattening-in-python) show how to do that. And then you can search for adjacent duplicates. One way to do that is to use [`groupby`](https://docs.python.org/3/library/itertools.html#itertools.groupby) – PM 2Ring Aug 04 '17 at 09:36
  • What you type is wrong. This two elements ['sw']] [['sw'] belong two DIFFERENT nested lists so they are not side by side. You have a big list that has nested lists as elements. What exactly do you want to achieve? To create a big list with unique elements? – Michail N Aug 04 '17 at 09:38
  • @MichailN my interpreter isn't complaining... – juanpa.arrivillaga Aug 04 '17 at 09:40
  • 2
    You should post your own code attempt, even though it doesn't give the desired result. Questions like this without some code often attract downvotes. – PM 2Ring Aug 04 '17 at 09:41
  • @ juanpa.arrivillaga I mean logically wrong not syntactically – Michail N Aug 04 '17 at 09:41
  • @PM2Ring thank you!!! – Caroline D.P.N. Barbieri Aug 04 '17 at 09:43
  • @MichailN I think it's fairly obvious what Caroline wants to achieve, since she has posted the expected output for that sample input. Clearly, the output should be a list of lists, with any runs of identical sublists deleted. – PM 2Ring Aug 04 '17 at 09:44
  • @MichailN It is not exactly creating a large list with unique elements, it would be to create a list deleting equal and adjacent elements. – Caroline D.P.N. Barbieri Aug 04 '17 at 09:48
  • @MichailN has a valid point, the adjacent elements ['sw'], ['y', '1', 'P12'], ['sw'] and ['sw'], ['y', '2', 'NOT'], ['sw'], are not equal. The two ['sw'] in the original list are part of a nested element – Simon Black Aug 04 '17 at 09:52
  • @SimonBlack I made a single list, now I have [['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']] – Caroline D.P.N. Barbieri Aug 04 '17 at 09:56

2 Answers2

0

Can do this with list comprehension and enumerate (to get the index)

mylist=[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']]
print (mylist)

newlist=[e for i, e in enumerate(mylist) if mylist[i-1] != mylist[i]]
print (newlist)

Here's the output

[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']]
[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']]
Simon Black
  • 913
  • 8
  • 20
  • The problem is that when I have duplicate elements I need to remove both. And obtain the following: [['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']] – Caroline D.P.N. Barbieri Aug 04 '17 at 10:04
0

To remove the duplicate elements,

mylist=[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']]
print (mylist)

newlist=[]
for i, e in enumerate(mylist):
    try:
        if not (mylist[i-1] == mylist[i] or mylist[i] == mylist[i+1]):
            newlist.append(e)
    except IndexError:
        newlist.append(e)

print (newlist)

Gives the results

[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['sw'], ['sw'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01'], ['y', 'Xt']]
[['x', '2', 'NOT'], ['sw'], ['y', '1', 'P12'], ['y', '2', 'NOT'], ['sw'], ['x', 'P01']]
Simon Black
  • 913
  • 8
  • 20