+ 1
I want create a list that contain all x and y element, but what is my fault?
X = [2, 3, 5]; Y = [6, 8, 9]; Li = [i t for i in x for t in y]; Print(li)
7 Answers
+ 2
X = [2, 3, 5];
Y = [6, 8, 9];
Li = X + Y
print(Li)
+ 1
Wow! Thank you
0
Li = [y for x in [X, Y] for y in x]
0
Li = [*X, *Y]
0
X = [2, 3, 5];
Y = [6, 8, 9];
Li = [x for x in [*X, *Y]]
print(Li)
0
The use of `zip` function may help. You can choose to either create a list of tuples(x, y), or create a list of values of tuple(x, y)
X = [2, 3, 5];
Y = [6, 8, 9];
list_of_tuples = list(zip(X,Y)) # creates a list of tuples
print(list_of_tuples)
extracted_tuple_values = []
for t in zip(X, Y):
extracted_tuple_values.extend(t) # adds content of each tuple in zip(X, Y)
print (extracted_tuple_values)