0

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?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by sravan953
0

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)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.