Python Online Compiler

							
							

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


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



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


difference between the two lists.



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


access the index of a list.



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


Convert the list characters to string



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


find the index of an item in a specified list.



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


flatten a shallow list.


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



select an item randomly from a list.


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


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

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


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)



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)



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



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)


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)