× 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { ?> ">
  • iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

1.Write a Python program to sum all the items in a list.



def sum_items(lst):
    sum = 0
    for x in lst:
        sum += x
    return sum
print(sum_items([1,-2,-28]))





                                

Output:

2. Write a Python program to multiplies all the items in a list.



def multiply_items(items):
    total = 1
    for x in items:
        total *= x
    return total
print(multiply_items([1,2,-8]))




                                

Output:

3. Write a Python program to get the largest number from a list.



def max_num( list ):
    max = list[0]
    print(max)
    for x in list:
        if x > max:
            max = x
    return max
print(max_num([10, 20, 8, 90]))



                                

Output:

4. Write a Python program to get the Smallest number from a list.




def smallest_num( list ):
    m = list[ 0 ]
    for a in list:
        if a < m:
            m = a
    return m
print(smallest_num([81, 2, -98, 90]))


                                

Output:

5. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.



def match_wordslist(words):
  char = 0

  for w in words:
    if len(w) > 1 and w[0] == w[-1]:
      char += 1
  return char

print(match_wordslist(['abcd', 'abcd', 'abca', '1221']))


                                

Output:

6. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.



def last(n): return n[-1]

def sort_list_last(tuples):
  return sorted(tuples, key=last)

print(sort_list_last([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]))


                                

Output:

7. Check the given list is empty or not.



lst = [12]
if not lst:
  print("List is empty")
else:
    print("Lis is not empty")


                                

Output:

8. Copy the one list to another list



def copy():
    old = [10, 22, 44, 23, 4]
    new = list(old)
    print(old)
    print(new)

copy()

                                

Output:

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



def long_wordsList(n, str):
    lenth = []
    txt = str.split(" ")
    for x in txt:
        if len(x) > n:
            lenth.append(x)
    return lenth    
print(long_wordsList(5, "Welcome to python class"))



                                

Output:

10. Find the geven lists are atlest one common number or not



def smae_data(list1, list2):
     output = False
     for x in list1:
         for y in list2:
             if x == y:
                 output = True
                 return output
print(smae_data([11,22,33,44,55], [55,66,77,88,99]))
print(smae_data([1,2,3,4,5], [6,7,8,9]))

                                

Output:

11. remove elements from list based on the specific index values



lst = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
lst = [x for (i,x) in enumerate(lst) if i not in (1,4,5)]
print(lst)
                                

Output:

12. How to generate a 3*4*6 3D array in list



lst = [[ ['+' for col in range(6)] for col in range(4)] for row in range(3)]
print(lst)


                                

Output:

13. Remove odd number in a given list


lst = [1,2,3,4,5,6,7,8,9]
lst = [x for x in lst if x%2==0]
print(lst)



                                

Output:

14. Write a Python program to shuffle and print a specified list.


from random import shuffle
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
shuffle(color)
print(color)



                                

Output:

15. Display the first and last 5 elements where the values are squares in a given range


def values():
    l = list()
    for i in range(1,30):
        l.append(i**2)
    print(l[:5])
    print(l[-5:])
    print(l[:])

values()



                                

Output:

16. Display the elements where the values are squares in a given range (except first 5 elements)


def values():
    l = list()
    for i in range(1,10):
        l.append(i**2)
    print(l[5:])
    
values()





                                

Output:

17. Write a Python program to generate all permutations of a list in Python.


import itertools
print(list(itertools.permutations([1,2,3,4])))



                                

Output:

18. difference between the two lists.


lst1 = [1, 2, 3, 4,5]
lst2 = [1, 2,4]
print(list(set(lst1) - set(lst2)))




                                

Output:

19. access the index of a list.


lst = [5, 15, 35, 8, 98]
for index, num in enumerate(lst):
    print(index, num)




                                

Output:

20. Convert the list characters to string


lst = ['a', 'b', 'c', 'd','e']
string = ''.join(lst)
print(string)



                                

Output:

21. find the index of an item in a specified list.


lst =[10, 300, 40, -6]
print(lst.index(40))




                                

Output:

22. flatten a shallow list.


import itertools
old = [[22,34,53],[11,35,65], [91], [173,94,0]]
new = list(itertools.chain(*old))
print(new)





                                

Output:

23. select an item randomly from a list.


import random
lst = ['Red', 'Blue', 'Green', 'Orange', 'Pink']
print(random.choice(lst))



                                

Output:

24. Second smallest number in a given list


def second_smallest_number(num):
  if (len(num)<2):
    return "not"
  if ((len(num)==2)  and (num[0] == num[1]) ):
    return "not"
  dup_num= set()
  uniq_num = []
  for x in num:
    if x not in dup_num:
      uniq_num.append(x)
      dup_num.add(x)
  uniq_num.sort()    
  return  uniq_num[1]

print(second_smallest_number([12,45,67,3,56,6,67]))



                                

Output:

25.Second largest number in a given list


def second_largest_number(num):
  if (len(num)<2):
    return "not"
  if ((len(num)==2)  and (num[0] == num[1]) ):
    return "not"
  dup_num= set()
  uniq_num = []
  for x in num:
    if x not in dup_num:
      uniq_num.append(x)
      dup_num.add(x)
  uniq_num.sort()    
  return  uniq_num[-2]

print(second_largest_number([12,45,67,3,56,6,67]))




                                

Output:

26. get unique values from a list.


lst = [10, 20, 30, 40, 20, 50, 60, 40]
print("Original List ",lst)
mset=set(lst)
lst=list(mset)
print("List of unique numbers ",lst)



                                

Output:

27. get the frequency of the elements in a list.


import collections
lst = [100,100,100,10,200,200,20,20,400,40,50,500,30]
print("Original List is : ",lst)
lst = collections.Counter(lst)
print("Frequency of the elements in the List  is: ",lst)



                                

Output:

28. count the number of elements in a specified range


def Rain(li, min, max):
    c = 0
    for x in li:
        if x>=min and x<=max:
            c += 1
    return c

list1 = [10,20,30,40,40,40,80,80,99]
print(Rain(list1, 40, 70))


                                

Output:

29. list of multiple integer into single integer


lst = [113, 343, 550]
print("Original List: ",lst)
x = int("".join(map(str, lst)))
print("Single Integer: ",x)


                                

Output:

30. Write a Python program to split a list based on first character of word.


from itertools import groupby
from operator import itemgetter

word_list = ['be','have','do','say','get','make','go','know','take','see','come','think',
     'look','want','give','use','find','tell','ask','work','seem','feel','leave','call']

for letter, words in groupby(sorted(word_list), key=itemgetter(0)):
    print(letter)
    for word in words:
        print(word)


                                

Output: