Tuesday, 20 September 2016

List Overlap With Example Explained - practicepython Exercise - 5

This is the 5th exercise of practicepython

Exercise:

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.
Extras:
  1. Randomly generate two lists to test this
  2. Write this in one line of Python
I have solved this in 2 ways. Conventional for loop and in one line statement:


For loop Solution


output:




One line Solution:



output:








GitHub Reference:




Github code for loop:


github code for single line:


https://gist.github.com/pinkpretty/ac7ace0e48d519456cd59c1e4a97465a
https://gist.github.com/pinkpretty/b6f1762efcb37a4f69e6f576505e7e39


Happy coding ! :)



1 comment:

  1. 1xbet korean ✔️ 1xbet korean ✔️ 1xbet korean - Legalbet
    In betting, one or скачать 1xbet more bets will be added to your wager before the match. This is a type of bet, in which the winner will place a bet of 1x

    ReplyDelete