Removing a list element that repeats itself -- Solved

In summary, the conversation discusses finding a solution for removing repeated sequences from an array. The desired result is to have an array that contains only the first occurrence of the repeating sequence. The conversation mentions trying to use the set() function but not being able to, and also discusses a potential solution involving creating sublists and checking for a repeating pattern. The conversation ends with the person admitting to not writing the solution themselves and receiving help from someone else.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
 
Technology news on Phys.org
  • #2
Your problem statement seems ill defined. How long must the sequence be before you remove it? In your example, "0" is repeated 4 times.
 
  • Like
Likes FactChecker and jedishrfu
  • #3
oh umm I solved it..but thanks
 
  • Like
Likes jedishrfu
  • #4
@Arman777 Can you share your solution so others can benefit?
 
  • Like
Likes jedishrfu
  • #5
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
 

Related to Removing a list element that repeats itself -- Solved

1. How do I remove a list element that repeats itself?

To remove a list element that repeats itself, you can use a loop to iterate through the list and use the remove() method to delete the duplicate element. Another option is to use the set() function to remove duplicates and then convert the set back to a list.

2. Can I remove multiple repeating elements from a list?

Yes, you can remove multiple repeating elements from a list by using the remove() method or the set() function as mentioned in the previous answer. However, keep in mind that the remove() method will only remove the first occurrence of the duplicate element.

3. Will removing a repeating list element change the order of the list?

It depends on the method you use to remove the repeating element. If you use the remove() method, the order of the list will remain the same. However, if you use the set() function, the order of the list may change as sets are unordered data structures.

4. How do I know if a list has repeating elements?

You can check if a list has repeating elements by converting it to a set and then comparing the length of the set to the length of the original list. If they are not equal, it means the list has repeating elements. Another approach is to use a loop to iterate through the list and use the count() method to check how many times each element appears in the list.

5. Are there any built-in functions to remove repeating elements from a list?

Yes, there are built-in functions such as remove() and set() that can help remove repeating elements from a list. Additionally, you can also use list comprehensions or the filter() function to remove duplicates from a list.

Similar threads

  • Programming and Computer Science
Replies
2
Views
664
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
43
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
Back
Top