Combine lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Combine lists

How do I combine lists so that each element of each list appears only once?

21st Aug 2022, 5:06 PM
Sonja
2 Answers
+ 7
Sonja , thera are many ways to accomplish this task. 2 of them are: lst1 = [1, 2, 3, 4] lst2 = [4, 5, 2, 7] result should be: [1, 2, 3, 4, 5, 7] (1) we can use the plus operator to merge lst1 and lst2. then make the resulting list a set, and convert it back to a list. (2) we can use a for loop. append elements from lst2 to lst1 only if these elements don't exist already in in lst1
21st Aug 2022, 7:57 PM
Lothar
Lothar - avatar
21st Aug 2022, 6:13 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar