LIST Assignment 2024
LIST Assignment 2024
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
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:
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 lsearch(alist,key):
p=0
for i in alist:
p=p+1
if i==key:
return p
else:
return -1
if index<0:
print("element not found")
else:
print("element found position is ",index)
Output:
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]