In: Computer Science
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples:
Sum all the items in a list.
Multiply all the items in a list.
Get the largest number from a list.
Get the smallest number from a list.
Remove duplicates from a list.
Check a list is empty or not.
Clone or copy a list.
Find the list of words that are longer than n from a given list of words.
Take two lists and returns True if they have at least one common member.
Print a specified list after removing the 0th, 4th and 5th
elements.
Sample List: ['Red', 'Green', 'White', 'Black', 'Pink',
'Yellow']
Expected Output: ['Green', 'White', 'Black']
Print the numbers of a specified list after removing even numbers from it.
Shuffle and print a specified list.
Get the difference between the two lists.
Convert a list of characters into a string.
Find the index of an item in a specified list.
Append a list to the second list.
Select an item randomly from a list.
Find the second smallest number in a list.
Find the second largest number in a list.
Get unique values from a list.
Get the frequency of the elements in a list.
Count the number of elements in a list within a specified range.
Check whether a list contains a sub list.
Create a list by concatenating a given list which range goes
from 1 to n.
Sample list : ['p', 'q'], n = 5
Sample Output : ['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4',
'p5', 'q5']
Find common items from two lists.
Change the position of every n-th value with the (n+1)th in a
list.
Sample list: [0, 1, 2, 3, 4, 5]
Expected Output: [1, 0, 3, 2, 5, 4]
Convert a list of multiple integers into a single integer.
Sample list: [11, 33, 50]
Expected Output: 113350
Split a list based on the first character of a word.
Select the odd items of a list.
Insert an element before each element of a list.
Print all elements of a nested lists (each list on a new line) using the print() function.
Split a list every Nth element.
Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n']
Expected Output: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k',
'n'], ['c', 'f', 'i', 'l']]
Create a list with infinite elements.
Concatenate elements of a list.
Convert a string to a list.
Replace the last element in a list with another list.
Sample data : [1, 3, 5, 7, 9, 10], [2, 4, 6, 8]
Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]
Check if the n-th element exists in a given list.
Find a tuple with the smallest second index value from a list of tuples.
Insert a given string at the beginning of all items in a
list.
Sample list: [1,2,3,4], string: emp
Expected output: ['emp1', 'emp2', 'emp3', 'emp4']
Find the list in a list of lists whose sum of elements is the
highest.
Sample lists: [1,2,3], [4,5,6], [10,11,12], [7,8,9]
Expected Output: [10, 11, 12]
Find all the values in a list are greater than a specified number.
Extend a list without append.
Sample data: [10, 20, 30]
[40, 50, 60]
Expected output: [40, 50, 60, 10, 20, 30]
Remove duplicates from a list of lists.
Sample list : [[10, 20], [40], [30, 56, 25], [10, 20], [33],
[40]]
New List : [[10, 20], [30, 56, 25], [33], [40]]
Program Code Screenshot:
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to copy:
import random
#sample list be l
l=[1,2,3,4,5,6]
print("The addition is:",sum(l))#prints the sum of elements
m=1
for i in l:
m=m*i
print("The multiplication is:",m)#prints the multiplication of list
elements
print(max(l))#print the largest element from list
print(min(l))#print the smallest element from list
l=list(set(l))#removes duplicates
if len(l)==0:
print("The list is empty")
else:
print("The list is not empty")
cloned_list=l[:]
print("The cloned list is:",cloned_list)
def list_of_words(l,n):
res=[]
for i in l:
if len(i)>n:
res.append(i)#append if length of word is greater than n
return res
print(list_of_words(["wordone","word"],5))
def compare_list(l1,l2):
for i in l1:
if i in l2:
return True
return False
print(compare_list([1,2,3],[3,4,5]))
l=['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
l.pop(0)
l.pop(3)#remove the elements of the specified indexes
l.pop(3)
print("The list after removing 0,4,5 indexed elements:",l)
l=[1,2,3,4,5,6]
print("The list is:",l)
for i in l:
if i%2==0:
l.remove(i)
print("After removing even numbers :",l)
l=[1,2,3,4,5,6]
print("The list is :",l)
print("The shuffled list is :",random.shuffle(l))
l1=[1,2,3,4,5,6,7]
l2=[4,5,6,7,8,9,0]
print("The difference is:")
res=[i for i in l1+l2 if i not in l1 or i not in l1]
print(res)
l=['a','b','c']
print("The string from list ",l,"is :",''.join(l))
print("The index of 'a' is :",l.index('a'))#prints the index of a
Note:
Please let me know in case of any help needed in the comments section.
Please see that as per the guidelines multiple questions can not be solved posted under a single question.
Only multiple choice type questions can be solved(Upto 4
questions). Thank you.