Lists: Store and Filter a Set of Items
Build a list of sample file names, keep only the .log entries, and read the first and last matches.
Based on the official Python documentation. Date checked: November 27, 2026. Script tested on Python 3.13.5.
Series: Python for small scripts, Part 5 of 10. Previous: Part 4: Loops with for and while. Next: Part 6: Dictionaries: Keys, Values, and Lookup.
TL;DR
- A list holds items in order, inside square brackets.
- This script keeps only the names that end with
.log, then prints the count, the first item, and the last item. - A tuple looks similar but cannot be changed after you create it.
- Tested on Python 3.13.5. This is Part 5 of 10.
Before you start
- Python 3.11 or newer. This script was tested on Python 3.13.5. Check with
python3 --versionorpython --version. - Work in the
python-scriptsfolder from Part 1. Parts 3 and 4 introducedifandfor. - Run with
python3 logs.py. On Windows,python logs.pyis the usual form.
The finished script
Save this as logs.py in that folder:
names = ["app.log", "auth.log", "cron.log", "debug.txt"]
logs = []
for name in names:
if name.endswith(".log"):
logs.append(name)
print(logs)
print("Count:", len(logs))
print("First:", logs[0])
print("Last:", logs[-1])
When you run it, you should see:
['app.log', 'auth.log', 'cron.log']
Count: 3
First: app.log
Last: cron.log
Steps
- Save and run the script. Change into
python-scriptsand runpython3 logs.py. Why: you can see the filtered list before you change the names. You should see: the four lines above.debug.txtis absent because it does not end with.log. - Read the empty list and
append.logs = []starts an empty list.name.endswith(".log")is true for the first three names, andlogs.append(name)adds each match to the end. Why: you can build a shorter list from a longer one without editing the originalnameslist. - Read
len,[0], and[-1].len(logs)is the number of items, here 3. Indexes start at 0, sologs[0]is the first item.logs[-1]is the last item. Why: these are the usual ways to count a list and to read either end. - A short note on tuples. A tuple uses parentheses, for example
pair = ("app.log", 12). You can readpair[0], but you cannot assign to it. A line such aspair[0] = "auth.log"raisesTypeError: 'tuple' object does not support item assignment. Use a list when the contents may change.
If it fails
IndexError: list index out of range. The index is past the end of the list, or the list is empty. This script printed that error forlogs[3]whenlogshad only one item (the only valid index was 0):
Traceback (most recent call last):
File "indexerror.py", line 2, in <module>
print(logs[3])
~~~~^^^
IndexError: list index out of range
Print len(logs) first, and remember that the last valid index is len(logs) - 1.
TypeError: 'tuple' object does not support item assignment. You tried to change an item in a tuple. Use a list if you need to append or replace items.- The printed list still includes
debug.txt. Theifcheck may be missing, or the suffix inendswithmay not match the file name exactly, including the dot.
Stay safe
- These names are samples. The script does not open, rename, or delete files on your disk.
- Filtering only builds a new list in memory. It does not change the files themselves.
- Check
len(logs)before you use a hard-coded index, especially if the list can be empty.
Next
Part 6: Dictionaries: Keys, Values, and Lookup
Sources
- Python docs, “Lists” (docs.python.org/3/tutorial/introduction.html#lists) and “More on Lists” (docs.python.org/3/tutorial/datastructures.html#more-on-lists), Python 3.15.2 documentation, checked November 27, 2026.
- Python docs, “Tuples and Sequences” (docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences), checked November 27, 2026.
Spot something wrong or outdated? Leave a comment below. Every comment is reviewed to keep these posts accurate.
By commenting, you agree to our Terms of use and confirm this is your own work. Short quotes are fine with a credit and link. Copied, unsafe, or abusive comments are removed. See something copied? Say so here.