2

I was trying to loop through two list which contain two different user, the for loops works when there's item in both list, but it won't works when one of the list doesn't have any item.

customer_list = [["ctm1","Jackson","abc"],["ctm2","Kaijun","edf"]]
  admin_list  = [["adm1","Jackson","martinez"],["adm2","Littsen","Lit"]]

for customer,admin in zip(customer_list, admin_list):
     print(customer,admin)

No item in admin_list

customer_list = [["ctm1","Jackson","abc"],["ctm2","Kaijun","edf"]]
admin_list = []

for customer,admin in zip(customer_list, admin_list):
     print(customer,admin)
Jackson Tai
  • 513
  • 4
  • 17

3 Answers3

2

Just need to implement some if’s. Example

a=[]

if a:
  

Put after your lists but before your for

2

Zip function will take the shortest length of array. You need to check null or empty in this case.

Please check reference. How to iterate through two lists in parallel?

Md. Mehedi Hasan
  • 196
  • 2
  • 14
2

just check if empty or not

l1 = [["ctm1","Jackson","abc"],["ctm2","Kaijun","edf"]]
l2 = [["adm1","Jackson","martinez"],["adm2","Littsen","Lit"]]


l3 = zip(l1, l2) if l1 and l2 else l1 if l1 else l2

for x in l3:
    print(x)

Ali Aref
  • 1,893
  • 12
  • 31