Python program on list comprehension, enumerate Pract 5

List comprehension and enumerate

1.Write a Python program to define a function that takes two lists and return True if they have at least one common member.

2.Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.

3.Write a Python program to print the numbers of a specified list after removing even numbers from it.

4.Write a Python program to clone or copy a list.

5.Write a Python program to show use on enumerate() in list comprehension

6.Write a python program to show use on enumerate() and its return value

Practical 4a : Write a Python program to define a function that takes two lists and return True if they have at least one common member.

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a Python program to define a function that takes two lists and return True if they have at least one common member.
"""
def common(itv1, itv2):
     result=False
     for x in itv1:
          for y in itv2:
               if x == y:
                    result = True
     return result    
print("common([1,2,3,4,5], [5,6,7,8,9]) gives output as",common([1,2,3,4,5], [5,6,7,8,9]))  
print("common([1,2,3,4,5], [6,7,8,9]) gives output as",common([1,2,3,4,5], [6,7,8,9])) 

OUTPUT

 >>> 
common([1,2,3,4,5], [5,6,7,8,9]) gives output as True
common([1,2,3,4,5], [6,7,8,9]) gives output as False
>>>  

Practical 4b : Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.
"""
shades = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
for (i,val) in enumerate(shades):
    if i not in (0,4,5):
        print("value at position",i,"is",val)

OUTPUT

 >>> 
value at position 1 is Green
value at position 2 is White
value at position 3 is Black
>>>  

Practical 4c : Write a Python program to print the numbers of a specified list after removing even numbers from it.

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a Python program to print the numbers of a specified list after removing even numbers from it.
"""
itv = [7,8, 120, 25, 44, 20, 27]

for x in itv:
    if x%2!=0:
        itv=x
        print("The numbers of a specified list after removing even numbers from it")
        print(itv)



OUTPUT

 >>> 
The numbers of a specified list after removing even numbers from it
7
The numbers of a specified list after removing even numbers from it
25
The numbers of a specified list after removing even numbers from it
27
>>> 



Practical 4d : Write a Python program to clone or copy a list.

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a Python program to clone or copy a list.
"""
original_list = [10, 22, 44, 23, 4]
#method 1
new_list1 = list(original_list)
print('Original List:',original_list)
print('New List using built-in list():',new_list1)

#method 2
new_list2=original_list.copy()
print('Original List:',original_list)
print('New List using copy():',new_list2)

#method 3
new_list3 = original_list[:]
print('Original List:',original_list)
print('New List using slice operator [:]:',new_list3)

OUTPUT

 Original List: [10, 22, 44, 23, 4]
New List using built-in list(): [10, 22, 44, 23, 4]
Original List: [10, 22, 44, 23, 4]
New List using copy(): [10, 22, 44, 23, 4]
Original List: [10, 22, 44, 23, 4]
New List using slice operator [:]: [10, 22, 44, 23, 4] 

Practical 4e : Write a python program to show use on enumerate() in list comprehension

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a python program to show use on enumerate() in list comprehension.
"""
itvoyagers=['I','could','do','this','all','day']
itv=[x for (i,x) in enumerate(itvoyagers) if i not in (0,2,4)]
print("Original list : ",itvoyagers)
print("List obtained as output is : ",itv)

OUTPUT

 >>> 
Original list :  ['I', 'could', 'do', 'this', 'all', 'day']
List obtained as output is :  ['could', 'this', 'day']

Enumerate function

To see syntax and use of enumerate() you can write help() on ‘>>>’ or prompt.
Then type enumerate in ‘help>’

>>> help()
help> enumerate

SYNTAX

enumerate(iterable[, start])

EXPLAINATION

Return an enumerate object.The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument.
Enumerate is useful for obtaining an indexed list:

(0, seq[0]), (1, seq[1]), (2, seq[2]), …

Practical 4f : Write a python program to show use on enumerate() and its return value

"""
Author : ITVoyagers (https://itvoyagers.in)

Date :09th August 2019

Description :  Write a python program to show use on enumerate() and its return value
"""
site=['i','t','v','o','a','g','e','r','s']
for (i,val) in enumerate(site):
    print("Output of enumerate() is", (i,val))

OUTPUT

Output of enumerate() is (0, 'i')
Output of enumerate() is (1, 't')
Output of enumerate() is (2, 'v')
Output of enumerate() is (3, 'o')
Output of enumerate() is (4, 'a')
Output of enumerate() is (5, 'g')
Output of enumerate() is (6, 'e')
Output of enumerate() is (7, 'r')
Output of enumerate() is (8, 's')
>>> 

Leave a Comment