Removing neighboring duplicates in list in python [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • Python - how to remove duplicates only if consecutive in a string?

    9 answers




I convert string a to a list and I want the loop to create ‍tabb = ['a', 'b', 'c', 'a']



a = aaabbbbcccaaa

taba = list(a)
tabb =

for i in taba:
for j in range(len(tabb)):
if not i[j] == i[j-1]:
tabb.append(i[j])

print (tabb)


But apparently my solution gives tabb =



Do You have any better and simple ideas to make it work?










share|improve this question















marked as duplicate by alex, Jon Clements list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 15:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    @alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
    – Ev. Kounis
    Nov 22 at 15:21










  • @Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
    – Jon Clements
    Nov 22 at 15:28










  • @JonClements There is one good one but it is regex specific. There has to be one though.
    – Ev. Kounis
    Nov 22 at 15:30












  • @Ev.Kounis thought I also saw a groupby?
    – Jon Clements
    Nov 22 at 15:30










  • @JonClements this one maybe?
    – Ev. Kounis
    Nov 22 at 15:36

















up vote
1
down vote

favorite













This question already has an answer here:




  • Python - how to remove duplicates only if consecutive in a string?

    9 answers




I convert string a to a list and I want the loop to create ‍tabb = ['a', 'b', 'c', 'a']



a = aaabbbbcccaaa

taba = list(a)
tabb =

for i in taba:
for j in range(len(tabb)):
if not i[j] == i[j-1]:
tabb.append(i[j])

print (tabb)


But apparently my solution gives tabb =



Do You have any better and simple ideas to make it work?










share|improve this question















marked as duplicate by alex, Jon Clements list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 15:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    @alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
    – Ev. Kounis
    Nov 22 at 15:21










  • @Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
    – Jon Clements
    Nov 22 at 15:28










  • @JonClements There is one good one but it is regex specific. There has to be one though.
    – Ev. Kounis
    Nov 22 at 15:30












  • @Ev.Kounis thought I also saw a groupby?
    – Jon Clements
    Nov 22 at 15:30










  • @JonClements this one maybe?
    – Ev. Kounis
    Nov 22 at 15:36















up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • Python - how to remove duplicates only if consecutive in a string?

    9 answers




I convert string a to a list and I want the loop to create ‍tabb = ['a', 'b', 'c', 'a']



a = aaabbbbcccaaa

taba = list(a)
tabb =

for i in taba:
for j in range(len(tabb)):
if not i[j] == i[j-1]:
tabb.append(i[j])

print (tabb)


But apparently my solution gives tabb =



Do You have any better and simple ideas to make it work?










share|improve this question
















This question already has an answer here:




  • Python - how to remove duplicates only if consecutive in a string?

    9 answers




I convert string a to a list and I want the loop to create ‍tabb = ['a', 'b', 'c', 'a']



a = aaabbbbcccaaa

taba = list(a)
tabb =

for i in taba:
for j in range(len(tabb)):
if not i[j] == i[j-1]:
tabb.append(i[j])

print (tabb)


But apparently my solution gives tabb =



Do You have any better and simple ideas to make it work?





This question already has an answer here:




  • Python - how to remove duplicates only if consecutive in a string?

    9 answers








python python-3.x list element






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:18









Ali AzG

607515




607515










asked Nov 22 at 15:15









baqterya

61




61




marked as duplicate by alex, Jon Clements list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 15:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by alex, Jon Clements list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 15:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    @alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
    – Ev. Kounis
    Nov 22 at 15:21










  • @Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
    – Jon Clements
    Nov 22 at 15:28










  • @JonClements There is one good one but it is regex specific. There has to be one though.
    – Ev. Kounis
    Nov 22 at 15:30












  • @Ev.Kounis thought I also saw a groupby?
    – Jon Clements
    Nov 22 at 15:30










  • @JonClements this one maybe?
    – Ev. Kounis
    Nov 22 at 15:36
















  • 1




    @alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
    – Ev. Kounis
    Nov 22 at 15:21










  • @Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
    – Jon Clements
    Nov 22 at 15:28










  • @JonClements There is one good one but it is regex specific. There has to be one though.
    – Ev. Kounis
    Nov 22 at 15:30












  • @Ev.Kounis thought I also saw a groupby?
    – Jon Clements
    Nov 22 at 15:30










  • @JonClements this one maybe?
    – Ev. Kounis
    Nov 22 at 15:36










1




1




@alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
– Ev. Kounis
Nov 22 at 15:21




@alex OP wants the second 'a' in this example. The proposed duplicate is wrong in this case.
– Ev. Kounis
Nov 22 at 15:21












@Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
– Jon Clements
Nov 22 at 15:28




@Ev.Kounis not sure it's incorrect... the answers cover various approaches for unique characters for an entire string and also unique consecutive characters. I'm open to another more specific duplicate if you have one to mind?
– Jon Clements
Nov 22 at 15:28












@JonClements There is one good one but it is regex specific. There has to be one though.
– Ev. Kounis
Nov 22 at 15:30






@JonClements There is one good one but it is regex specific. There has to be one though.
– Ev. Kounis
Nov 22 at 15:30














@Ev.Kounis thought I also saw a groupby?
– Jon Clements
Nov 22 at 15:30




@Ev.Kounis thought I also saw a groupby?
– Jon Clements
Nov 22 at 15:30












@JonClements this one maybe?
– Ev. Kounis
Nov 22 at 15:36






@JonClements this one maybe?
– Ev. Kounis
Nov 22 at 15:36














1 Answer
1






active

oldest

votes

















up vote
1
down vote













groupby from itertools is your ally:



from itertools import groupby

a = 'aaabbbbcccaaa'

res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']




The solution without any libraries (the one you were trying to arrive at) would be:



res = [a[0]]

for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)


which has the same outcome of course.






share|improve this answer





















  • Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
    – baqterya
    Nov 22 at 17:12












  • post this as a separate question. It is beneficial for the community
    – Ev. Kounis
    Nov 22 at 17:16










  • also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
    – Ev. Kounis
    Nov 22 at 17:20


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













groupby from itertools is your ally:



from itertools import groupby

a = 'aaabbbbcccaaa'

res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']




The solution without any libraries (the one you were trying to arrive at) would be:



res = [a[0]]

for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)


which has the same outcome of course.






share|improve this answer





















  • Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
    – baqterya
    Nov 22 at 17:12












  • post this as a separate question. It is beneficial for the community
    – Ev. Kounis
    Nov 22 at 17:16










  • also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
    – Ev. Kounis
    Nov 22 at 17:20















up vote
1
down vote













groupby from itertools is your ally:



from itertools import groupby

a = 'aaabbbbcccaaa'

res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']




The solution without any libraries (the one you were trying to arrive at) would be:



res = [a[0]]

for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)


which has the same outcome of course.






share|improve this answer





















  • Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
    – baqterya
    Nov 22 at 17:12












  • post this as a separate question. It is beneficial for the community
    – Ev. Kounis
    Nov 22 at 17:16










  • also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
    – Ev. Kounis
    Nov 22 at 17:20













up vote
1
down vote










up vote
1
down vote









groupby from itertools is your ally:



from itertools import groupby

a = 'aaabbbbcccaaa'

res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']




The solution without any libraries (the one you were trying to arrive at) would be:



res = [a[0]]

for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)


which has the same outcome of course.






share|improve this answer












groupby from itertools is your ally:



from itertools import groupby

a = 'aaabbbbcccaaa'

res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']




The solution without any libraries (the one you were trying to arrive at) would be:



res = [a[0]]

for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)


which has the same outcome of course.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 15:16









Ev. Kounis

10.4k21544




10.4k21544












  • Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
    – baqterya
    Nov 22 at 17:12












  • post this as a separate question. It is beneficial for the community
    – Ev. Kounis
    Nov 22 at 17:16










  • also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
    – Ev. Kounis
    Nov 22 at 17:20


















  • Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
    – baqterya
    Nov 22 at 17:12












  • post this as a separate question. It is beneficial for the community
    – Ev. Kounis
    Nov 22 at 17:16










  • also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
    – Ev. Kounis
    Nov 22 at 17:20
















Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
– baqterya
Nov 22 at 17:12






Thank you, it worked flawlessly! BTW I'm trying to make reverse function and I almost made it work but it seems that it can't reach the first element of array: goo.gl/3cHkWN ( link to code in google docs) /// If input = AAABBBBCAAAaaDD, string = A3B4CA3a2D2 decompressed should be AAABBBBAAAaaDD but is BBBBAAAaaDD. Can You see why the loop can't see taba[2-1]?
– baqterya
Nov 22 at 17:12














post this as a separate question. It is beneficial for the community
– Ev. Kounis
Nov 22 at 17:16




post this as a separate question. It is beneficial for the community
– Ev. Kounis
Nov 22 at 17:16












also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
– Ev. Kounis
Nov 22 at 17:20




also note that the fact that you do not have a 1 after the 'C' makes it much more complicated than it has to be. If you have control over the format of the input string, please add it.
– Ev. Kounis
Nov 22 at 17:20



Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo