-3

#im new to cs and was trying to solve (Take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.)

how can i remove the duplicate 1 from the output ?

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for x in a:
    if x in b:
        print(x)
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
Yigit148
  • 11
  • 4
  • Convert them to sets and then use the same code you are using – Dani Mesejo Sep 22 '21 at 10:21
  • Use a set: `print(set(a).intersection(b))` – Matthias Sep 22 '21 at 10:22
  • Assuming this isn't stupidly restricted homework, and order doesn't matter (or it's always sorted, as in this case), converting to `set`s would be the obvious solution. Possibly with `sorted` involved later to output in a useful order. – ShadowRanger Sep 22 '21 at 10:23
  • 2
    Even the input data is the same https://stackoverflow.com/q/47454788/4046632 – buran Sep 22 '21 at 10:24
  • print(set(a) & set(b)) –  Sep 22 '21 at 10:26
  • @buran https://www.practicepython.org/solution/2014/03/19/05-list-overlap-solutions.html its an exercise from this site – Yigit148 Sep 22 '21 at 10:26
  • Thank you everyone converting to set solved it – Yigit148 Sep 22 '21 at 10:28
  • @buran i have only been coding for the last 2 days and i found some solutions but they were very different than my solution wanted to know what i could do to get the output from my solution. im new to all this and i dont really know how to use stackoverflow etc. – Yigit148 Sep 22 '21 at 10:30
  • Take [tour], visit [help-center](https://stackoverflow.com/help), check [ask] – buran Sep 22 '21 at 10:32

1 Answers1

0
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

# join list a and list b to one list
joined_list = a + b

#  remove duplicated
order_list = list(set(joined_list))
print(order_list)
# output
# [1, 2, 3, 34, 5, 4, 6, 8, 7, 9, 10, 11, 13, 12, 21, 55, 89]
ofek
  • 19
  • 3