Member Avatar for sravan953

Hey guys,

I have some code here which checks the last three characters in the list:

site_list=
['C', ':', '\\', 'D', 'o', 'c', 'u', 'm', 'e', 'n', 't', 's', ' ', 'a', 'n', 'd', ' ', 'S', 'e', 't', 't', 'i', 'n', 'g', 's', '\\', 'S', 'r', 'a', 'v', 'a', 'n', '\\', 'M', 'y', ' ', 'D', 'o', 'c', 'u', 'm', 'e', 'n', 't', 's', '\\', 'M', 'y', ' ', 'M', 'u', 's', 'i', 'c', '\\', 'T', 'a', 'p', 'h', 'a', ' ', 'N', 'i', 'a', 'n', 'g', '.', 'm', 'p', '3']

def check_dot():
    p=0
    for a in site_list:
        if a=='.':
            p=site_list.index('.')
            for b in site_list:
                s3=''.join(site_list[p])
                p+=1
                if (p>len(site_list)):
                    break

check_dot()

I used the above code to check whether the list ends with a '.mp3' or '.exe', is my code right? Any more ideas on how I can shorten it?

Any more ideas on how I can shorten it?

How about using slicing:

site_list=
['C', ':', '\\', 'D', 'o', 'c', 'u', 'm', 'e', 'n', 't', 's', ' ', 'a', 'n', 'd', ' ', 'S', 'e', 't', 't', 'i', 'n', 'g', 's', '\\', 'S', 'r', 'a', 'v', 'a', 'n', '\\', 'M', 'y', ' ', 'D', 'o', 'c', 'u', 'm', 'e', 'n', 't', 's', '\\', 'M', 'y', ' ', 'M', 'u', 's', 'i', 'c', '\\', 'T', 'a', 'p', 'h', 'a', ' ', 'N', 'i', 'a', 'n', 'g', '.', 'm', 'p', '3']

def check_dot(my_list):
    return ''.join(my_list[-3:])

print check_dot(site_list)
Member Avatar for sravan953

Thanks a lot! It worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.