Next


Python Online Compiler

							
							

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]))

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]))

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]))

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]))

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.
                      

idef 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']))

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)]))


Check the given list is empty or not.

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

Copy the one list to another list


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

copy()


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"))

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]))

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)

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)



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)

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)



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()