0% found this document useful (0 votes)
12 views5 pages

LIST Assignment 2024

The document provides Python programming solutions for various list operations, including removing duplicates, finding the second largest number, searching elements using linear and binary search, moving duplicates to the end of a list, and summing corresponding elements from two lists. Each solution is accompanied by code snippets and example outputs demonstrating the functionality. The document serves as a practical guide for handling common list manipulations in Python.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

LIST Assignment 2024

The document provides Python programming solutions for various list operations, including removing duplicates, finding the second largest number, searching elements using linear and binary search, moving duplicates to the end of a list, and summing corresponding elements from two lists. Each solution is accompanied by code snippets and example outputs demonstrating the functionality. The document serves as a practical guide for handling common list manipulations in Python.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment (Based on List):

List Methods:
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
Add the elements of a list (or any iterable), to the end of the current
extend() list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

1. Write a Python program to remove duplicates from an integer list(Elements supplied


by the user). Find 2nd largest number from the list.
Solution:

def Remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list

# Driver Code
duplicate=[]
n = int(input("Enter number of elements : "))
for i in range(0, n):
ele = int(input())
duplicate.append(ele) # adding the element
ulist=Remove(duplicate)
print(ulist)
ulist.sort()
print("2nd Largest number number",ulist[-2])

Output:
Enter number of elements : 4
2
43
2
5
[2, 43, 5]
2nd Largest number number 5

2. Write a Python program to find the list of words that are longer than n from a given
list of words.
Solution:

def long_words(n, str):


word_len = []
txt = str.split(" ")
for x in txt:
if len(x) > n:
word_len.append(x)
return word_len

lst=input("Enter a sentence")
wordl=int(input("Enter length of the word"))
print(long_words(wordl, lst))
Output:
Enter a sentencethe quick brown fox jumps over the lazy dog
Enter length of the word4
['quick', 'brown', 'jumps']

3. Write a program to input integer values in a list and perform Linear Search or Binary
Search. Also display the position of the number in the list.
Solution:

def bsearch(alist, key):


start = 0
end = len(alist)
while start < end:
mid = (start + end)//2
if alist[mid] > key:
end = mid
elif alist[mid] < key:
start = mid + 1
else:
return mid
return -1

def lsearch(alist,key):
p=0
for i in alist:
p=p+1
if i==key:
return p
else:
return -1

print("1. Linear search 2. Binary Search")


ch=int(input("Enter your choice"))
alist = input('Enter the list of numbers: ')
alist = alist.split()
alist = [int(x) for x in alist]
key = int(input('Enter the number to search for: '))
if ch==1:
index = lsearch(alist, key)
elif ch==2:
alist.sort()
index=bsearch(alist,key)

if index<0:
print("element not found")
else:
print("element found position is ",index)
Output:

1. Linear search 2. Binary Search

Enter your choice1

Enter the list of numbers: 5 3 2 6 8 31

Enter the number to search for: 8

element found position is 5

1. Linear search 2. Binary Search

Enter your choice2

Enter the list of numbers: 4 2 6 1 9 0

Enter the number to search for: 9

element found position is 5


4. Write a program to move all duplicate values in a list to the end of the list.

Solution
l = eval(input("Enter the list: "))
dedup = []
dup = []
for i in l:
if i in dedup:
dup.append(i)
else:
dedup.append(i)

l = dedup + dup

print("Modified List:")
print(l)

Output
Enter the list: [20, 15, 18, 15, 7, 18, 12, 13, 7]
Modified List:
[20, 15, 18, 7, 12, 13, 15, 18, 7]

5. Write a program that takes any two lists L and M of the same size and adds their
elements together to form a new list N whose elements are sums of the corresponding
elements in L and M. For instance, if L = [3, 1, 4] and M = [1, 5, 9], then N should equal
[4,6,13].

Solution
print("Enter two lists of same size")
L = eval(input("Enter first list(L): "))
M = eval(input("Enter second list(M): "))
N = []

for i in range(len(L)):
N.append(L[i] + M[i])

print("List N:")
print(N)

Output
Enter two lists of same size
Enter first list(L): [3, 1, 4]
Enter second list(M): [1, 5, 9]
List N:
[4, 6, 13]

You might also like