Jul-21-2019, 12:49 AM
Alright, I had a problem where I needed to make two random lists, then make a third list which would contain the elements that the first 2 lists shared. I did that:
Secondly, there is apparently a way to solve this problem with 1 line of code. How? What is the trick for that?
import random a = [] b = [] c = [] i = 0 while i < 13: y = random.randint(0, 20) a.append(y) i += 1 i = 0 while i < 14: y = random.randint(0, 20) b.append(y) i += 1 print("This is list a: " + str(a)) print("This is list b: " + str(b)) for x in a: if x in b: if x not in c: c.append(x) print(str(c) + " are element in both a and b lists")Now lines 7 through 15 where I make the random lists feel slightly clunky and make me feel that there is a better way to do what I did in them. Is that correct, if so how?
Secondly, there is apparently a way to solve this problem with 1 line of code. How? What is the trick for that?