TYL - P3 Lists and Files Discussion (largest, remove duplicates, at least 1 common element)

TYL - P3 Lists and Files Discussion

 (Largest Number, Remove Duplicates, common element)


1. Write a Python program to get the largest number from a list.

use max() on a list to find the largest number or use a variable.

Program:

def largestNumber(t):
    return max(t)

elements=[48,57,69,58,23,3]
largest = largestNumber(elements)

print("Largest Number:", largest)

When you use a variable, you can initially set it to None.

Program:

def largestNumber(t):
    largest = None
    for e in t:
        if largest == None or largest<e:
            largest=e

    return largest

elements=[48,57,69,58,23,3]
largest = largestNumber(elements)

print("Largest Number:", largest)

Output:

python tyl_lists.py
Largest Number: 69



2. Write a Python program to remove duplicates from a list.


If you use the function set() it will automatically remove duplicates as it is it's implied functionality.  However, it returns an object of type "set".
So, we convert it back to list using list()
Eg.
>>> s=set([1,1,2])
>>> s
{1, 2}
>>> type(s)
<class 'set'>
>>> t=list(s)
>>> t
[1, 2]
>>> type(t)
<class 'list'>


Program:

def removeDuplicates(t):
    return list(set(t))

elements=[48,57,69,58,23,58,69,3]
wodup=removeDuplicates(elements)
print("Without Duplicates:", wodup)

Solution without using sets

Create an empty list.
Use an appropriate loop to remove items from the existing list.
only add to new list if the element is not already present.
you can do it this way to practice some functions learnt in class such as pop() append().

Program

def removeDuplicates(t):
    #return list(set(t))
    wodup=[]
    while t:
        e=t.pop()
        if e not in wodup:
            wodup.append(e)
           
    return wodup

elements=[48,57,69,58,23,58,69,3]
wodup=removeDuplicates(elements)
print("Without Duplicates:", wodup)


Output:

Without Duplicates: [3, 69, 58, 23, 57, 48]


3. Write a Python function that takes two lists and returns True if they have at least one common member.

loop through each element in either of the two lists
use the "in" operator to check if that element is in the other list.
If in a function, return True.  It would stop the loop there
Or set up a boolean variable, set it initially to False.  Once a common element is found, set it to True, break out of the loop, return the variable.

Program:

def checkCommon(t1, t2):

    for e in t1:
        if e in t2:
            return True

    return False

listOne=[1,3,5,6,4]
listTwo=[3,4,5,8]
listThree=[56,78,45]
print("List One and List Two have a common element:",checkCommon(listOne, listTwo) )
print("List One and List Three have a common element:",checkCommon(listOne, listThree) )


With a variable

def checkCommon(t1, t2):

    common=False
    for e in t1:
        if e in t2:
            common=True
            break

    return common


Output:

List One and List Two have a common element: True
List One and List Three have a common element: False

Comments

Popular posts from this blog

TYL - Food Corner Program

Classes and objects solution

TYL - Salary Hike - Python Problem