How to loop through item in two list in python one by one

Viewed 94

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)
3 Answers

Just need to implement some if’s. Example

a=[]

if a:
  

Put after your lists but before your for

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)

Related