Categorizing input data in Python lists


Introduction

A List is a type of data structure in Python language that can store elements of different data types within these “[]” brackets. The given data are listed according to the data type like integer and string. There are three primary functions involved namely filtering, sorting, and grouping. To sort lists based on certain conditions such as alphabetical order or ascending or descending numerical value we can use sorted() which takes two parameters the object to be sorted and the key parameter for the sorting technique.

Categorizing input data in Python lists

The empty list can also be initialized to include the results. When the list is named as list_1 and the empty list is represented as,

List_1 =[]

The list can hold the elements of different data types like,

List_1 = [“hello”, 1, ‘a’, 78.9].

The list can hold another list inside the brackets along with other elements like,

List_1 = [“hello”,[4,5,6], [‘a’]]

Approach

Approach 1 − Using isinstance() function

Approach 2 − Using sorting function

Approach 3 − Using type() function

Approach 1: Python Program to categorize input data using isinstance() and filter() function

The filter() function is used to get the desired data type elements from the given list and the lambda function is defined with arguments “a” and returns the result in Boolean variables.

Algorithm

  • Step 1 − The variables are defined with values as “book”, 23, “note”, 56, “pen” and 129.

  • Step 2 − The filter() function usually comes along with the lambda to check for data using the letter or parameter “a”.

  • Step 3 − The other way of doing it is with the help of isinstance() and filter() to filter the elements according to the elements.

  • Step 4 − When the below code runs, it returns the input according to the specific data type.

Example

#initialize the list with strings and integers data type
list_1 = ["book", 23, "note",56, 'pen', 129]

#filtering the strings items in the list and adding them
strings = list(filter(lambda a: isinstance(a, str), list_1))
#finding the number items in the list
num = list(filter(lambda a: isinstance(a, (int, float)), list_1))

#printing the categorized input data
print("Strings:", strings)
print("Numbers:", num)

Output

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 129]

Approach 2: Python Program to categorize input data using isinstance() and sorting function

The isinstance() function is initialized with two arguments, to check whether the elements are present in the list and the sort() function is used to sort the elements in some order.

Algorithm

  • Step 1 − The variables are defined with values as “book”, 23, “note”, 56, “pen” and 129.

  • Step 2 − Another list data structure is defined to store the variables returned.

  • Step 3 − The other way of doing it is with the help of isinstance() and sorted() to sort the elements according to the elements.

  • Step 4 − When the below code runs, it returns the input according to the specific data type.

Example

#initialize the list with strings and integers data type
list_1 = ["book", 23, "note",56, 'pen', 129]
#declaring empty lists
strings = []
num = []
#defining the pro and iterating through the list_1
for pro in list_1:
   #finding the strings items in the list and adding them
   if isinstance(pro, str):
      strings.append(pro)
   else:
      #finding the number items in the list and adding them
      num.append(pro)
#using the sorting function
strings_sorted = sorted(strings)
num_sorted = sorted(num)

#printing the categorized input data
print("Strings:", strings_sorted)
print("Numbers:", num_sorted)

Output

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 34.5]

Approach 3: Python Program to categorize input data using type() function

The type() function is used to categorize the elements based on the element type in the list.

Algorithm

  • Step 1 − Initialize the list containing number and string elements.

  • Step 2 − Define the empty two lists to store the numbers and string elements separately.

  • Step 3 − for loop can be used to iterate through the list data structure.

  • Step 4 − The other way to do it is with the help of for loop and type() to check the elements according to the elements.

  • Step 5 − When the below code runs, it returns the input according to the specific data type.

Example

#initializes the list with strings and integers data types
list_1 = ["book", 23, "note", 56, 'pen', 129]
#empty lists are initialized
strings = []
num = []
#for loop is used to iterate through the list using type() function
for a in list_1:
   if type(a) == str:
      strings.append(a)
   else:
      num.append(a)
#printing the categorized input data
print("Strings:", strings)
print("Numbers:", num)

Output

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 129]

Conclusion

Python is a versatile and high-level language that the user can easily understand. Three different approaches are illustrated in the article to categorizing input data in Python lists.

Updated on: 25-Aug-2023

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements