How do i check if a list only contains other lists in python?
- User
How do i check if a list only contains other lists in python?
Assistant
Here's how to check if a Python list only contains other lists:
pythondef contains_only_lists(my_list): return all(isinstance(item, list) for item in my_list) # Examples list1 = [[], [1, 2], ["a", "b"]] # Contains only lists list2 = [[1, 2], "string", [3, 4]] # Contains a string list3 = [[1, 2], 5, [3, 4]] # Contains a number print(contains_only_lists(list1)) # True print(contains_only_lists(list2)) # False print(contains_only_lists(list3)) # FalseWould you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.