Categorizing input data in Python lists

Categorizing input data in Python lists means separating list elements by their data types (strings, numbers, etc.). Python provides several built-in functions like isinstance(), type(), and filter() to accomplish this task efficiently.

What is Data Categorization?

Data categorization involves grouping list elements based on their data types. For example, separating strings from numbers in a mixed list ?

mixed_data = ["book", 23, "note", 56, "pen", 129, 34.5]
print("Original list:", mixed_data)
Original list: ['book', 23, 'note', 56, 'pen', 129, 34.5]

Using isinstance() with filter()

The isinstance() function checks if an object belongs to a specific data type. Combined with filter() and lambda, it provides an efficient way to categorize data ?

# Initialize the list with mixed data types
mixed_data = ["book", 23, "note", 56, "pen", 129, 34.5]

# Filter strings using isinstance() and lambda
strings = list(filter(lambda x: isinstance(x, str), mixed_data))

# Filter numbers (both int and float)
numbers = list(filter(lambda x: isinstance(x, (int, float)), mixed_data))

print("Strings:", strings)
print("Numbers:", numbers)
Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 129, 34.5]

Using isinstance() with Loop and Sorting

This approach iterates through the list, categorizes elements, and sorts them for better organization ?

# Initialize the list with mixed data types
mixed_data = ["book", 23, "note", 56, "pen", 129, 34.5]

# Create empty lists for categorization
strings = []
numbers = []

# Iterate and categorize elements
for item in mixed_data:
    if isinstance(item, str):
        strings.append(item)
    elif isinstance(item, (int, float)):
        numbers.append(item)

# Sort the categorized data
strings_sorted = sorted(strings)
numbers_sorted = sorted(numbers)

print("Sorted Strings:", strings_sorted)
print("Sorted Numbers:", numbers_sorted)
Sorted Strings: ['book', 'note', 'pen']
Sorted Numbers: [23, 34.5, 56, 129]

Using type() Function

The type() function directly checks the exact type of an element. This approach is more specific than isinstance() ?

# Initialize the list with mixed data types
mixed_data = ["book", 23, "note", 56, "pen", 129, 34.5]

# Create empty lists for categorization
strings = []
integers = []
floats = []

# Categorize using type() function
for item in mixed_data:
    if type(item) == str:
        strings.append(item)
    elif type(item) == int:
        integers.append(item)
    elif type(item) == float:
        floats.append(item)

print("Strings:", strings)
print("Integers:", integers)
print("Floats:", floats)
Strings: ['book', 'note', 'pen']
Integers: [23, 56, 129]
Floats: [34.5]

Comparison of Methods

Method Performance Flexibility Best For
isinstance() + filter() Fast High Functional programming style
isinstance() + loop Medium High Complex categorization logic
type() Fast Medium Exact type checking

Advanced Example: Multiple Data Types

# Complex list with various data types
complex_data = ["hello", 42, 3.14, [1, 2, 3], {"key": "value"}, True, None]

# Categorize by data type
categorized = {}
for item in complex_data:
    data_type = type(item).__name__
    if data_type not in categorized:
        categorized[data_type] = []
    categorized[data_type].append(item)

for data_type, items in categorized.items():
    print(f"{data_type}: {items}")
str: ['hello']
int: [42]
float: [3.14]
list: [[1, 2, 3]]
dict: [{'key': 'value'}]
bool: [True]
NoneType: [None]

Conclusion

Use isinstance() with filter() for functional programming style, isinstance() with loops for complex logic, and type() for exact type matching. Choose the method based on your specific requirements and coding style preferences.

Updated on: 2026-03-27T13:36:51+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements