Search for a symmetrical inner portion of a list What the code does: Takes a Python list of integers as input and searches for a 'symmetrical' inner-portion of the list. Example:

symmetrical_sum([10,8,7,5,9,8,15]) == ([8, 7, 5, 9, 8], 37) 


Note: Symmetry occurs if the value of the ith element from the start of the list is equal to the value of the ith element from the end of the list.

def symmetrical_sum(a):
'''Takes a Python list of integers as input and searches for a 'symmetrical' inner-portion of the list 

Example: symmetrical_sum([10,8,7,5,9,8,15]) == ([8, 7, 5, 9, 8], 37) 

Symmetry occurs if the value of the ith element at the start of the list is equal to the value of the 
ith element at the end of the list'''  

#extract duplicate value
dupe = [x for n, x in enumerate(a) if x in a[:n]] 

#if no duplicate values found, do the following:
if dupe == []:
    middle = float(len(a))/2
    if middle % 2 != 0:
        sym = a[int(middle - .5):int(middle + .5)]
        ans = a[int(middle - .5)]
        tuple1 = (sym,ans)
    elif middle % 2 == 0:
        sym = a[int(middle - 1):int(middle + 1)]
        ans = sum(a[int(middle - 1):int(middle + 1)])//2
        tuple1 = (sym,ans)
    return tuple1
else:
    d_to_i = int("".join(map(str, dupe))) #convert duplicate value to integer
    p1 = a.index(d_to_i) #get index of first duplicate
    p2 = a.index(d_to_i, p1+1) #get index of second duplicate
    sym = a[p1:p2+1] #[symmetrical-portion]
    ans = sum(sym) #sum-of-symmetrical-portion
    tuple2 = (sym, ans)

return tuple2


Looking at my code I realize that it could probably be more efficient than this. It works, but I'm sure it could be better. I just don't possess the skills to 'compress it'. I've submitted my code already so this is just for my own personal development.
The code in the question is not working as intended.



  Symmetry occurs if the value of the ith element at the start of the list is equal to the value of the 
  ith element at the end of the list


Meaning for any list l the symmetry occurs when l[i] == l[~i].

&gt;&gt;&gt; symmetrical_sum([1, 2, 1, 3, 4])
([1, 2, 1], 4)
&gt;&gt;&gt; symmetrical_sum([0, 1, 2, 1, 3, 4])
([1, 2, 1], 4)


This says that the midpoint is the same distance from the end of the list as the starting value. And it says 1 and -3 are the same distance.
No where does it say the input will only have one duplicate.

&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4, 2, 1])
ValueError: 21 is not in list
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4, 1, 2])
ValueError: 12 is not in list
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 1, 4, 2])
ValueError: 12 is not in list

The output returns different amount of values when the size of the input are different multiples of two.

&gt;&gt;&gt; symmetrical_sum([1, 2])
([1], 1)
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4])
([2, 3], 2)
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4, 5, 6])
([3], 3)
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4, 5, 6, 7, 8])
([4, 5], 4)
&gt;&gt;&gt; symmetrical_sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
([5], 5)