I want create a list that contain all x and y element, but what is my fault? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

27th Jun 2020, 7:31 PM
Abbakar_ah!!!
Abbakar_ah!!! - avatar
6 Answers
+ 2
X = [2, 3, 5]; Y = [6, 8, 9]; Li = X + Y print(Li)
27th Jun 2020, 7:37 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
+ 1
Wow! Thank you
27th Jun 2020, 7:47 PM
Abbakar_ah!!!
Abbakar_ah!!! - avatar
0
Li = [y for x in [X, Y] for y in x]
27th Jun 2020, 7:40 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
0
Li = [*X, *Y] ย ย 
27th Jun 2020, 7:41 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
0
X = [2, 3, 5]; Y = [6, 8, 9]; Li = [x for x in [*X, *Y]] print(Li)
27th Jun 2020, 7:43 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
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)
28th Jun 2020, 1:19 AM
Ipang