Hello guys I have this type of data in list in list
array = [
["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AAHATA","GHORAY SHAH","KOT KHAWAJA SAEED",
],
["002188", 0, 0, 0, 0, 0, "2", "3"],
["PRODUCT NAME PACK","BAGHBAN PURA","GAWAL MANDI","OLD ANARKALI","MAYO HOSPITAL","GARHI SHAHU BAZAR","BILAL GUNJ","LADY WELLINGTON HOSPITAL",
],
["002188", "3", 0, 0, 0, 0, 0, 0],
["008999", 0, 0, 0, 0, "1", 0, 0],
["012961", 0, 0, 0, 0, 0, "3", 0],
]
Each list that contain "PRODUCT NAME PACK"
has actual city name and their values are placed in next list like "BAIGAM KOT"
has value 0 after the item code "002188". I want to loop through it into table in which it get the value in each row like that
[["002188", "BAIGAM KOT",0],["002188","FIAZ BAGH" ,0],["002188", "OLD ANARKALI",0]["002188", "SULTAN PURA",0],["002188","TEZAB AAHATA" ,0],["002188","GHORAY SHAH", "2"],["002188", "KOT KHAWAJA SAEED","3"]]
You need for
-loop with if/else
to run different code for different lists.
For list with "PRODUCT NAME PACK"
you have to keep list with names in variable - so you may use it in next loops when you get list with numbers
For other list you can keep first number as index
and rest use in zip(names, numbers)
to create pairs (name, number)
which can be used to create [index, name, number]
array = [["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AAHATA","GHORAY SHAH","KOT KHAWAJA SAEED",],["002188", 0, 0, 0, 0, 0, "2", "3"],["PRODUCT NAME PACK","BAGHBAN PURA","GAWAL MANDI","OLD ANARKALI","MAYO HOSPITAL","GARHI SHAHU BAZAR","BILAL GUNJ","LADY WELLINGTON HOSPITAL",],["002188", "3", 0, 0, 0, 0, 0, 0],["008999", 0, 0, 0, 0, "1", 0, 0],["012961", 0, 0, 0, 0, 0, "3", 0],
]# --- convert ---result = []
#names = None # default value at start for data in array:if data[0] == "PRODUCT NAME PACK":names = data[1:]else:index = data[0]numbers = data[1:]for name, number in zip(names, numbers):result.append( [index, name, number] )# --- display ---for item in result:print(item)
Result:
['002188', 'BAIGAM KOT', 0]
['002188', 'FIAZ BAGH', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'SULTAN PURA', 0]
['002188', 'TEZAB AAHATA', 0]
['002188', 'GHORAY SHAH', '2']
['002188', 'KOT KHAWAJA SAEED', '3']
['002188', 'BAGHBAN PURA', '3']
['002188', 'GAWAL MANDI', 0]
['002188', 'OLD ANARKALI', 0]
['002188', 'MAYO HOSPITAL', 0]
['002188', 'GARHI SHAHU BAZAR', 0]
['002188', 'BILAL GUNJ', 0]
['002188', 'LADY WELLINGTON HOSPITAL', 0]
['008999', 'BAGHBAN PURA', 0]
['008999', 'GAWAL MANDI', 0]
['008999', 'OLD ANARKALI', 0]
['008999', 'MAYO HOSPITAL', 0]
['008999', 'GARHI SHAHU BAZAR', '1']
['008999', 'BILAL GUNJ', 0]
['008999', 'LADY WELLINGTON HOSPITAL', 0]
['012961', 'BAGHBAN PURA', 0]
['012961', 'GAWAL MANDI', 0]
['012961', 'OLD ANARKALI', 0]
['012961', 'MAYO HOSPITAL', 0]
['012961', 'GARHI SHAHU BAZAR', 0]
['012961', 'BILAL GUNJ', '3']
['012961', 'LADY WELLINGTON HOSPITAL', 0]