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
9 Antworten
+ 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
+ 1
list1 = ["a", "f", "u", 1]
list2 = ["y", "r", 2]
ind = -1
for x in list2:
ind += 2
list1.insert(ind, x)
print(list1)
0
i completely forgot you can do that heh
0
yeah i know
0
also yeah its not the required output but that's something they can parse themselves like slicing only part where you need them
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"]