Removing a list element that repeats itself -- Solved

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Element List
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 1K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have an array that looks like

A=[0,0,1,1,2,5,6,3,7,7,0,0,1,1,2,5,6,3,7,7]

since the "0,0,1,1,2,5,6,3,7,7" part reapeats itself I don't need the second part so for a given array it should give me

A=[0,0,1,1,2,5,6,3,7,7]

I can't use the set() function and I don't know what else I can use in this case.Is there a function which can do this operation ?

I tried to write a code but I couldnt
 
Physics news on Phys.org
jim mcnamara said:
@Arman777 Can you share your solution so others can benefit?
Python:
def check_repeat(l):
    for i in range(1,len(l)//2+1):
        pattern=l[:i]
        sub_lists=create_sublists(l, i, True)
        if all([x==pattern for x in sub_lists]):
            print("Found pattern {} with length {}".format(pattern, i))
            return patterndef create_sublists(l, n, only_full=False):
    sub_lists=[]
    for j in range(n, len(l),n):
        if only_full and len(l[j:j+n])<n:
            continue
        sub_lists.append(l[j:j+n])
    return sub_lists

but I didnt write it myself. Someone helped me