+ 1
[Python] I have 2 lists how can I merge them in one list.
a=[1,2,3] b=[a,b] and I need to make a new list as new_list=[1a,1b,2a,2b,3a,3b] lists will be longer and its not easy to write new_list by hand. If somebody can help me I will be glad.thanx in advance
9 Réponses
+ 8
Art you can get modules using help("modules") but this doesn't work on Sololearn and you can use this code https://code.sololearn.com/c071JeXy6LcW/?ref=app
+ 7
from itertools import product
l1 = list('123')
l2 = list('ab')
for i in l1:
for j in l2:
print(i+j)
print()
for i in product(l1, l2):
print("".join(i))
+ 4
Roneel You can do everything by hand, but whenever there's a predefined module, it's usually more efficient to use it because it is optimized for the task
+ 3
Mert Yazıcı and Anna thank you very much but Im new in Python and dont have any experiences about any coding,Im watching the lessons but Im so confused about that,everytime we are importing something (is that moduls?) and how we know about what we can import? is there any list for that?
+ 3
You'll know the most important modules over time. You can read all about itertools here:
https://docs.python.org/3.1/library/itertools.html
+ 3
Thank you for your helps I need to study hard I guess :)
+ 2
https://code.sololearn.com/cqK0e320fJmk/?ref=app
Mert Yazıcı, you were 46 seconds faster 😩
+ 2
Anna why need to import itertools?
https://code.sololearn.com/c7Al59rc8PU6/?ref=app
+ 2
I learnt something new. thanks!