Need some help guy's | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need some help guy's

Let's explaining my idea firstly. I have 2 list : List1 = ["a", "f", "u", 1] List2 = ["y", "r, 2] I want add another list to append each elements in the list1 & list2 but like this : Full_list = ["a", "y","f","r"] That mean i want add every time one element from each list and append it to next. Element from other list. I use while loop, but he give me an errors

3rd Sep 2021, 2:47 AM
Mr. anrsaad
Mr. anrsaad - avatar
6 Answers
+ 2
something like this should do the trick: l1 = ["a", "f", "u", 1] l2 = ["y", "r", 2] for i in range(1, len(l1), 2): l1.insert(i, l2.pop(0)) print(l1) this iterates using range that determine the start, stop, step by 1, the length of the l1, 2 so in the first iteration its inserting element at the first index and second iteration would be the third index so it insert an element at the third index
3rd Sep 2021, 2:55 AM
CutieRei
CutieRei - avatar
+ 1
list1 = ["a", "f", "u", 1] list2 = ["y", "r", 2] ind = -1 for x in list2: ind += 2 list1.insert(ind, x) print(list1)
3rd Sep 2021, 9:10 PM
Mahad Ahmed
Mahad Ahmed - avatar
0
i completely forgot you can do that heh
3rd Sep 2021, 3:16 AM
CutieRei
CutieRei - avatar
0
yeah i know
3rd Sep 2021, 3:20 AM
CutieRei
CutieRei - avatar
0
also yeah its not the required output but that's something they can parse themselves like slicing only part where you need them
3rd Sep 2021, 3:21 AM
CutieRei
CutieRei - avatar
0
Mr. anrsaad is that the real full list? Because if it is then what your code needs is to iterate over range(2) and use indexing to get the first 2 elements on each list. Something like this list1 = ["a","f","u",1] list2 = ["y","r",2] full_list = [] for i in range(2): full_list.append(list1[i]) full_list.append(list2[i]) print(full_list) This code will output the full list based on your question ["a","y","f","r"]
3rd Sep 2021, 3:29 AM
raynard
raynard - avatar