Member Avatar for Member #531174

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?

Dani AI

Generated

OP wanted a reliable way to detect whether a character list representing a path ends with extensions like ".mp3" or ".exe". The loop in the first post is fragile: site_list.index('.') returns the first dot (not the last), ''.join(site_list[p]) is a misuse of join, and manual indexing risks out‑of‑range errors. 's slicing suggestion is concise and correct for many simple cases, but a few more robust options are worth considering.

Convert the character list once to a string and use the standard library to inspect the extension:

import os

s = ''.join(site_list)            # build the path string once
root, ext = os.path.splitext(s)   # ext includes the leading dot
if ext.lower() == '.mp3':
    # handle mp3

For checking multiple extensions in one test, str.endswith is compact and readable:

s = ''.join(site_list).lower()
if s.endswith(('.mp3', '.exe')):
    # matched extension

Notes and edge cases:

  • Joining the list once is fine; repeated joins inside loops are wasteful.
  • Use lower() or casefold() for case-insensitive checks.
  • Strip trailing whitespace if filenames might include it: s = s.rstrip().
  • os.path.splitext correctly handles filenames with multiple dots (it uses the last dot for the extension).
  • If the data started as a string, keep it a string rather than converting to a list of chars.

References: Python string endswith and os.path.splitext in the standard library provide the documented behavior used above: str.endswith and os.path.splitext.

Recommended Answers

All 2 Replies

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 Member #531174

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.