0

I intend to apply a function to a variable. I do not know beforehand if the incoming variable will be a list of list or a single list. e.g

var_name = [a, b, c]
or
var_name = [[a,b,c], [d,e,f], [h,i,j]]

The list of list could contain as many lists as possible. How do I go about verifying if the variable is a list or list of lists. I intend to apply a function depending on the type. I tried to use len(var_name) but the length for list will be the number of items in single list while for list of lists, it is the number of lists in the list of lists.

what I am trying to achieve is this:

#function for list
def apply_val(lst):
    do something with single list
#function for list of list
def apply_val2(lst):
    do something with list of lists

var_name = single_list or list_of_lists
if var_name == single list
    apply_val(single_list)
else:
    apply_val(list_of_lists)

How do I check if the var_name is a list or list of lists?

5
  • Does this answer your question? Checking if type == list in python Commented May 29, 2020 at 7:05
  • will the list of lists always be a list of lists, or a mix of lists and values? Commented May 29, 2020 at 7:07
  • See if the first element in the argument is a string. i.e. if isinstance(arg[0], str):. If not assume it's a list. Commented May 29, 2020 at 7:09
  • it may partially in the sense of using isinstance but not completely. How will I frame it in this sense for both list and list of lists? Commented May 29, 2020 at 7:09
  • Also the list of lists will always be a list of lists, not a mix for sure Commented May 29, 2020 at 7:11

3 Answers 3

2

list of lists does not mean anything, what is [["foo", "bar"], "baz"] ?

However, if you are sure you could only have "list of something that is not a list" and "list of list", just:

  • Check the list is not empty
  • Then, check the type of first item
if len(my_list) > 0:
  if isinstance(my_list[0], list):
    # List of list
    pass
  else:
    # Simple list
    pass
Sign up to request clarification or add additional context in comments.

2 Comments

This is a better approach, its recommended to use isinstance instead of type().
In general yes, but I think for this simple case where we're just looking at a basic list, it doesn't really make a difference
1

if the single list is always just values AND the list of lists always contains ONLY lists, you can check if it is a single list or list of lists like so:

list1 = [1,2,3]
list2 = [[1,2,3],[1,2,3]]

if type(list2[0]) == list:
    print('list of lists')
else:
    print('single list')

returns 'list of lists' for list2 and 'single list' for list1

if there is a mix you could do something like:

list3 = [1,[1,2,3], 4.343, 'string']
types = set([type(element) for element in list3])
if list in types and len(types) > 1:
    print('mixed')

1 Comment

accepted because of simplicity and the extra handling of mixed types
0

Try this

def check_list(test_list):
    if type(test_list[0]) == list:
        return True
    else:
        return False

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.