Assignment#7 - Mohammed Abdul Wasay
def indexOfSmallestElement(lst):
#check if elements is greater than 1
if len(lst)>1:
#get the minimum value in the list
smallest_value = min(lst)
#return the index of minimum value
smallestvalue_index=lst.index(smallest_value)
return smallestvalue_index
else:
print("List contains one or no items")
if __name__ == "__main__" :
#create and initialize a list
list1 = [23,56,32,89,21,44,51]
min_list1 = indexOfSmallestElement(list1)
print("Index of smallest value is ",min_list1)
Index of smallest value is 4
# function returns first occurance of most common of names in a list
def most_commons(lst):
return max(set(lst), key=lst.count)
List = ["Jane", "Aaron", "Cindy","Jane", "Aaron"]
most_commons(List)
'Jane'
# function returns first occurance of most common of names in a list using counter()
from collections import Counter
def Most_Common(lst):
data = Counter(lst)
return data.most_common(1)[0][0]
List1 = ["Jane", "Aaron", "Cindy","Jane", "Aaron"]
most_commons(List1)
'Jane'
# function returns occurance of most common of names in a list using counter()
from collections import Counter
def mostCommonNames(lstofnames):
mostcomm = {}
mostcomm = Counter(lstofnames)
#return mostcomm
print(mostcomm.most_common(2))
List = ["Jane", "Aaron", "Cindy", "Aaron","Jane"]
mostCommonNames(List)
[('Jane', 2), ('Aaron', 2)]
def isPalindromicList(a):
if a == a[::-1]:
print("The list is a palindrome")
else:
print("The list isn't a palindrome")
mylist = [77, 1, 56, 65, 1, 77]
print("The list is :")
print(mylist)
mylist = ' '.join([str(elem) for elem in mylist])
isPalindromicList(mylist)
The list is : [77, 1, 56, 65, 1, 77] The list is a palindrome