I am trying to make a function which return max from nested list?

后端 未结 6 1654
无人及你
无人及你 2021-01-24 08:48

I wrote this and its working fine with everything but when I have an empty list in a given list(given_list=[[],1,2,3]) it saying index is out of range. Any help?

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 09:42

    You are assuming given_list has at least 1 element which is incorrect. To avoid index out of range, you may add

    if (len(given_list) == 0)
      return None
    

    to the beginning of your function.

提交回复
热议问题