Wannes Aerts
Wannes Aerts

Reputation: 3

Insert from one list into another in python

I'm trying to insert from one list into another in python but i cant seem to get it working. I would like to insert the first line of list2 under the first line in list1 that contains 'A1033', then at the next line in list1 that contains 'A1033' insert the second line of list2 and so on. this is the code that i have:

with open('D:\\TranslateFIles\\Ext_Python.txt', 'r', encoding='utf-8') as f:
    list1 = f.readlines()
f.close()

with open('D:\\TranslateFIles\\A1033_Python.txt', 'r', encoding='utf-8') as f:
    list2 = f.readlines()
f.close()

outFile=open('D:\\TranslateFIles\\Output3_Python.txt', 'w', encoding='utf-8')

A1033 = 'A1033'
a2067 = 'A2067'

 
for line in list1:
    for line2 in list1:
        for element in list2:
            if A1033 in line:
                if not a2067 in line2:
                    list1.insert(list1.index(line2), element)
                    
            
for lines in list1:
    outFile.write(lines)
outFile.close()   

Snipped of what is in list1

T14-P2818-L30:Location
T14-P8629-A1033-L999:Location
T14-P8629-A2060-L999:Magasin
T14-P4960-V1000-P2818-L128:TransferRoute
T14-P4960-V1003-P2818-L128:WhseEmployee
T14-P4960-V1004-P2818-L128:WorkCenter
T14-P4960-V1001-P2818-L128:StockkeepingUnit
T14-P4960-X1-L999:
T14-F1-P2818-L128:Code
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2060-L999:Code
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2060-L999:Nom
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2060-L999:Code emplacement par d‚faut
T14-F5700-P2818-L128:Name 2
T14-F5700-P8629-A1033-L999:Name 2
T14-F5700-P8629-A2060-L999:Nom 2
T14-F5701-P2818-L128:Address
T14-F5701-P8629-A1033-L999:Address
T14-F5701-P8629-A2060-L999:Adresse
T14-F5702-P2818-L128:Address 2
T14-F5702-P8629-A1033-L999:Address 2
T14-F5702-P8629-A2060-L999:Adresse (2Šme ligne)
T14-F5703-P2818-L128:City
T14-F5703-P8629-A1033-L999:City
T14-F5703-P8629-A2060-L999:Ville
T14-F5704-P2818-L128:Phone No.

Snipped of list2

T14-P8629-A2067-L999:Locatie
T14-F1-P8629-A2067-L999:Code
T14-F2-P8629-A2067-L999:Naam
T14-F130-P8629-A2067-L999:Standaard opslaglocatiecode
T14-F5700-P8629-A2067-L999:Naam 2
T14-F5701-P8629-A2067-L999:Adres
T14-F5702-P8629-A2067-L999:Adres 2
T14-F5703-P8629-A2067-L999:Stad
T14-F5704-P8629-A2067-L999:Telefoon nr.
T14-F5705-P8629-A2067-L999:Telefoon nr.
T14-F5706-P8629-A2067-L999:Telex Nr.
T14-F5707-P8629-A2067-L999:Fax nr.
T14-F5713-P8629-A2067-L999:Contact
T14-F5714-P8629-A2067-L999:Postcode
T14-F5715-P8629-A2067-L999:County
T14-F5718-P8629-A2067-L999:E-mail
T14-F5719-P8629-A2067-L999:Startpagina
T14-F5720-P8629-A2067-L999:Land/Regio Code
T14-F5724-P8629-A2067-L999:Gebruik als in-transit
T14-F5726-P8629-A2067-L999:Vereisen
T14-F5727-P8629-A2067-L999:Pick
T14-F5728-P8629-A2067-L999:Cross-Dock Vervaldatum Calc.
T14-F5729-P8629-A2067-L999:Cross-Docking gebruiken
T14-F5730-P8629-A2067-L999:Ontvangst vereisen
T14-F5731-P8629-A2067-L999:Verzending vereisen
T14-F5732-P8629-A2067-L999:Bin Verplicht

Snipped of how i want list1 to look after the insert.

T14-P2818-L30:Location
T14-P8629-A1033-L999:Location
T14-P8629-A2067-L999:Locatie
T14-P8629-A2060-L999:Magasin
T14-P4960-V1000-P2818-L128:TransferRoute
T14-P4960-V1003-P2818-L128:WhseEmployee
T14-P4960-V1004-P2818-L128:WorkCenter
T14-P4960-V1001-P2818-L128:StockkeepingUnit
T14-P4960-X1-L999:
T14-F1-P2818-L128:Code
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2067-L999:Code
T14-F1-P8629-A2060-L999:Code
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2067-L999:Naam
T14-F2-P8629-A2060-L999:Nom
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2067-L999:Standaard opslaglocatiecode
T14-F130-P8629-A2060-L999:Code emplacement par d‚faut
T14-F5700-P2818-L128:Name 2
T14-F5700-P8629-A1033-L999:Name 2
T14-F5700-P8629-A2067-L999:Naam 2
T14-F5700-P8629-A2060-L999:Nom 2
T14-F5701-P2818-L128:Address

Upvotes: 0

Views: 56

Answers (2)

JarroVGIT
JarroVGIT

Reputation: 5304

I've wrote a little that might help you:

list1 = ["abc", "123", "ab1", "4542", "erf", "ab"]
list2 = ["insert1","insert2","insert3","insert4"]
new_list = []
index_list2 = 0

for item in list1:                             # loop over each item in list1.
    new_list.append(item)                      # always add the current item to the new list.
    if item.startswith("ab"):                  # Only in certain condition, add the next item of list2 to the new list.
        new_list.append(list2[index_list2])
        index_list2 += 1                       # Keep track of what item in list2 we have already added.

print(new_list)
# > ['abc', 'insert1', '123', 'ab1', 'insert2', '4542', 'erf', 'ab', 'insert3']

Let me know if this helps :)

Upvotes: 0

Epsi95
Epsi95

Reputation: 9047

Suppose l1 and l2 are you first two list and output is your result then

output = []
for i in l1:
    output.append(i)
    if 'A1033' in i:
        output.append(l2.pop(0))

Upvotes: 1

Related Questions