Mathew K Analytics

Lesson 30 · Python for Data Science

Master Selecting and Filtering Data in Python for Data Science

Python can help you find, select, and filter information with just a few lines of code. Knowing how to select and filter data is essential for working with…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb
 

Selecting and Filtering Data in Python#

Python can help you find, select, and filter information with just a few lines of code.

Knowing how to select and filter data is essential for working with lists, analyzing info, and creating useful programs.

By the end, you will pick out exactly what you want from a sea of data!

Let us dive in!

# Let us start by creating a simple list of numbers.
numbers = [2, 7, 4, 10, 3, 5, 8, 1, 6]
print(numbers)
[2, 7, 4, 10, 3, 5, 8, 1, 6]

Accessing specific elements#

We often want just one item from a list, not the whole thing.

# To get the first item, use index 0.
first_number = numbers[0]
print(first_number)

# To get the fourth item, use index 3.
fourth_number = numbers[3]
print(fourth_number)
2
10
# Let us use input so you can select a position yourself!
index = int(input('Type an index from 0 to 8: '))
selected = numbers[index]
print('You picked:', selected)
You picked: 4
 

Selecting a range with slicing#

Slicing lets you grab a part of a list by position.

The format is: list_variable[start:stop]

# Get a slice from the second to fourth number.
slice_example = numbers[1:4]
print(slice_example)
[7, 4, 10]

Filtering for specific values#

Sometimes we want to find all items that meet a rule.

Let us find all the even numbers in our list.

# Create an empty list for even numbers.
evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)
print(evens)
[2, 4, 10, 8, 6]
# We can also use a list comprehension for filtering.
odds = [n for n in numbers if n % 2 != 0]
print(odds)
[7, 3, 5, 1]

Practical: Filtering with user input#

You can make your own mini-filter using input().

Let us choose a threshold and get numbers bigger than that.

# Get a number from the user.
threshold = int(input('Show me all numbers greater than: '))
greater = [n for n in numbers if n > threshold]
print('These numbers are greater:', greater)
These numbers are greater: [7, 10, 8, 6]
 

Safe selection: Handling out-of-range indices#

If you try an invalid index, Python will stop your program.

Let us make it safer with a conditional.

# Try to select a number, but check if the index is valid.
pos = int(input('Pick an index: '))
if 0 <= pos < len(numbers):
    print('Your choice:', numbers[pos])
else:
    print('Sorry, that index is out of range.')
    
Sorry, that index is out of range.
 
# Another way: Use try-except to catch errors gracefully.
try:
    idx = int(input('Which index do you want? '))
    print('Here you go:', numbers[idx])
except IndexError:
    print('Oops! That spot is out of range.')
    
Here you go: 6
 

Advanced: Filtering with multiple conditions#

You can combine rules to be more selective.

Let us get numbers that are even and also greater than five.

# Filter numbers that are even and greater than five.
specials = [n for n in numbers if n % 2 == 0 and n > 5]
print(specials)
[10, 8, 6]
# Filtering works with words too!
words = ['cat', 'dog', 'rabbit', 'deer', 'cow', 'crow']
long_words = [w for w in words if len(w) > 3]
print(long_words)
['rabbit', 'deer', 'crow']

Sorting and filtering together#

You can combine sorting with your filters to get organized results.

Let us make a list of odd numbers, then sort them.

# Make a list of odd numbers and sort them.
my_odds = [n for n in numbers if n % 2 != 0]
my_odds.sort()
print(my_odds)
[1, 3, 5, 7]
# You can also use sorted() if you do not want to change the original list.
sorted_evens = sorted([n for n in numbers if n % 2 == 0])
print(sorted_evens)
# numbers list stays unchanged.
[2, 4, 6, 8, 10]

Real world mini-project: Filtering ages#

Let us say you have a group of everyone's ages at a party.

You want to count which guests are at least 18 years old.

# List of guest ages
ages = [5, 28, 18, 40, 16, 21, 10, 19]

# Adults are 18+
adults = [a for a in ages if a >= 18]
print('Adults:', adults)
Adults: [28, 18, 40, 21, 19]
# Try also getting the number of kids in one line.
kids = [a for a in ages if a < 18]
print('Kids:', kids)
Kids: [5, 16, 10]

Troubleshooting: What if nothing matches?#

Filtering can return an empty list if no items fit your rule.

Always check your results when writing new filter conditions.

# Try to filter for guests over one hundred years old.
over_100 = [a for a in ages if a > 100]
print(over_100)
[]

Tip: Filter with custom functions#

You can write your own function to test each item in a filter.

Let us filter ages using a is_even_age function.

# Function to check if an age is even.
def is_even_age(age):
    return age % 2 == 0

even_ages = [a for a in ages if is_even_age(a)]
print('Even ages:', even_ages)
Even ages: [28, 18, 40, 16, 10]

Recap: What did we learn?#

  • How to select by index and range in Python lists
  • How to filter with loops and comprehensions
  • Ways to sort filtered results
  • Handling errors and empty results
  • Real world examples like filtering ages

You can now pick, filter, and organize data for simple and powerful programs.

Thanks for watching!#

If you enjoyed this, hit like, leave a comment, subscribe for more, and share with a friend. Filtering lets you focus on what matters most in any big pile of data.

Happy coding!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.