Removing redundant lines from script Context: I've been learning Python for a few months and I'm able to write codes which do work, but they normally look very ugly and contain a lot of unnecessary code. But I normally struggle to find better ways to do things because of my limited knowledge.

I would like some advice on what I can improve on the code below. FYI - it works perfectly well, but I'm guessing there must be ways to improve it. I know I'm repeating myself in the loops, for example.

def getTableGroupTopMember_Test1(coordinates):
    '''
    Depending on the number of coordinates given, this function/code returns a unique     coordinate. 
    '''

    mylist = coordinates

    for adc in activeDataConnections:
        if mylist.Count == 1:
            for table in adc:
                if table.Name == mylist[0]:
                    print "/"
        elif mylist.Count == 2:
            for table in adc:
                if table.Name == mylist[0]:
                    for topgroup in table.TopAxis.Groups:
                        if topgroup.Name == mylist[1]:
                            print topgroup.Address
        elif mylist.Count == 3:
            for table in adc:
                if table.Name == mylist[0]:
                    for topgroup in table.TopAxis.Groups:
                        if topgroup.Name == mylist[1]:
                            for topmember in topgroup:
                                if topmember.Name == mylist[2]:
                                    print topmember.Address
        else:
            print "your code is shit!"


getTableGroupTopMember_Test1(["Table10"])
getTableGroupTopMember_Test1(["Table10", "profile_julesage",])
getTableGroupTopMember_Test1(["Table10", "profile_julesage", "_25_to_34"])


Output:

/
/2
/2[3]
This is what I would do.

First, rename coordinates to coord_list so you can eliminate mylist which is kind of useless since you only use immediately assign it to coordinates. To make things slightly simpler I assigned count to coord_list.Count so now you can just check count instead of coord_list.Count. Instead of executing the loop if the Count is bad, we check that count is between 1 and 3  before entering the loop. From here it loops though the activeDataConnections and has been changed so it checks count instead of repetition of the same logic.

Here is the code:

def getTableGroupTopMember_Test1(coord_list):
    '''
    Depending on the number of coordinates given, this function/code returns a unique coordinate. 
    '''
    count = coord_list.Count

    # thanks to 200_success
    if not 0 &lt; count &lt; 4:
        print "your code is shit!"
        return

    for adc in activeDataConnections:
        for table in adc:
            if table.Name == coord_list[0] and count == 1:
                print "/"
            elif table.Name == coord_list[0]:
                for topgroup in table.TopAxis.Groups:
                    if topgroup.Name == coord_list[1] and count == 2:
                        print topgroup.Address
                    elif topgroup.Name == coord_list[1]:
                        for topmember in topgroup:
                            if topmember.Name == coord_list[2]:
                                print topmember.Address



getTableGroupTopMember_Test1(["Table10"])
getTableGroupTopMember_Test1(["Table10", "profile_julesage",])
getTableGroupTopMember_Test1(["Table10", "profile_julesage", "_25_to_34"])