How to add item from a list to a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add item from a list to a list

a = [["line1", "arc1", "line2", "line3", "line4", "line5"], ["line6", "arc2", "line7"]] b = ["Layer1", "Layer2"] # result need to be: c = [["Layer1", "line1", "arc1", "line2", "line3", "line4", "line5"],["Layer2", "line6", "arc2", "line7"]]

26th Jul 2019, 11:55 AM
Eddy Lucas
Eddy Lucas - avatar
7 Answers
+ 5
probably not the best solution but it works: a = [["line1", "arc1", "line2"], ["line3", "arc2", "line4"]] b = ["Layer1", "Layer2"] c = [] d = [] res = [] c.append(b[0]) c.extend(a[0]) d.append(b[1]) d.extend(a[1]) res.append(c) res.append(d) print(res) # output: [['Layer1', 'line1', 'arc1', 'line2'], ['Layer2', 'line3', 'arc2', 'line4']] # result need to be: c = [["Layer1", "line1", "arc1", "line2"],["Layer2", "line3", "arc2", "line4"]] if you really need to have doublequotes around string you can use replace to do this
26th Jul 2019, 12:14 PM
Lothar
Lothar - avatar
+ 1
a[0].insert(0, b[0]) a[1].insert(0, b[1]) Assuming you are using Python...
26th Jul 2019, 12:11 PM
Trigger
Trigger - avatar
+ 1
Yep. Thats what I was talking about using for loop, Eddy Lucas
26th Jul 2019, 12:48 PM
Trigger
Trigger - avatar
0
You can even use a for loop
26th Jul 2019, 12:11 PM
Trigger
Trigger - avatar
0
Thnx Tomas. Yes it's pyton. The lists a and b vary in length in runtime
26th Jul 2019, 12:18 PM
Eddy Lucas
Eddy Lucas - avatar
0
Thnx for helping Thomas I tried this: for x in range(len(b)): a[x].insert(0, b[x]) print(a)
26th Jul 2019, 12:21 PM
Eddy Lucas
Eddy Lucas - avatar
0
~ swim ~ I'll try when im at work in about 3 weeks BTW the lists are a random nr of lines and or arcs. But are always a group or groups on a layer or layers
3rd Aug 2019, 7:05 PM
Eddy Lucas
Eddy Lucas - avatar