Performance of iterating through lists of lists but only some child lists have more than one element

  Kiến thức lập trình

For my use case, I have a big list of lists but only some have more than one or zero element.
Example:

my_list = [[2], [3], [1,2], [], [5], [3,4,5]]

I’m wondering, Is it better to just loop normally:

for cur_list in my_list:
   for element in cur_list:
       # do what ever

Or adding some logic to avoid creating for loop:

for cur_list in my_list:
   match len(cur_list):
      case 0:
         continue
      case 1:
         element = cur_list[0]
         # do what ever
      case _:
        for element in cur_list:
            # do what ever
   

This might just me overthinking, I’m just curious.

I don’t see any difference personally.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT