Create a Python Code That Takes Every Object From The List And Check if it Has a List Object or Not. if a List Object is Present in a List it Should be Unpacked And The Overall Count of The List Should be Conveyed. if List Object is Not Present in The Existing List it Should Say Cannot Unpack

In this article let's check if the list is having a nested list within it or not.  if the nested  list is present within the list then print the  count of the nested list.  Else print list cannot be unpacked.

We will be taking two samples of list declared in the program  in order to demo this

 

Create a Python Code That Takes Every Object From The List And Check if it Has a List Object or Not. if a List Object is Present in a List, it Should be Unpacked And The Overall Count of The List Should be Conveyed. if List Object is Not Present in The Existing List, it Should Say Cannot Unpack.

 

 

Create a Python Code That Takes Every Object From The List And Check if it Has a List Object or Not. if a List Object is Present in a List, it Should be Unpacked And The Overall Count of The List Should be Conveyed. if List Object is Not Present in The Existing List, it Should Say Cannot Unpack.

Define a function check_list_objects() and pass the input argument as a list to it.  iterate all the items present in the list and check if the instance is a list type.  if at least one instance is a list type then increment the counter variable total. Using the return statement within the variable total


def check_list_objects(items):
   total = 0
   for item in items:
       if isinstance(item, list):
           total += 1
   return total





In the main program take to list list1 and list2 and declare the list within the program shown below.

list1 = [[1, 2, 3], [4, 5, 6], [7, 8], [9]]
list2 = [10, 20, (30, 40), 50, 90 , "hello"]




Take a variable named as result and call the function check_list_objects() by passing list1. Store the return value of the function.  check if the result value is greater than zero then print the overall count of the nested list else print the list cannot be  unpacked.

result = check_list_objects(list1)
if result > 0:
   print("Overall Count of the List is : ", result)
else:
   print("List Object is Not Present, List Cannot be Unpacked")





Take another variable result2 and call the same function by passing the list as an input argument and note down the result print the nested list count  else print the list cannot be unpacked.

result2 = check_list_objects(list2)
if result2 > 0:
   print("Overall Count of the List is : ", result2)
else:
   print("List Object is Not Present, List Cannot be Unpacked")




Using the about two examples you might have understood  the first list contains nested list within it and the  second list doesn't contain any nested list,  so the second list cannot be unpacked

 




Complete Python program
==============================


def check_list_objects(items):
   total = 0
   for item in items:
       if isinstance(item, list):
           total += 1
   return total


list1 = [[1, 2, 3], [4, 5, 6], [7, 8], [9]]
list2 = [10, 20, (30, 40), 50, 90 , "hello"]

result = check_list_objects(list1)
if result > 0:
   print("Overall Count of the List is : ", result)
else:
   print("List Object is Not Present, List Cannot be Unpacked")

result2 = check_list_objects(list2)
if result2 > 0:
   print("Overall Count of the List is : ", result2)
else:
   print("List Object is Not Present, List Cannot be Unpacked")





Output
====================

Overall Count of the List is :  4
List Object is Not Present, List Cannot be Unpacked




Conclusion
======================

Modify the above  program having list,  remove the nested list according to your choice and run the program note down the results.


Comment down below if you have any suggestions or queries regarding the program.






Post a Comment

0 Comments