32. Problem to get the difference between two lists.

In this program, we will take two lists as input. Find the difference between two lists with the help of the below-given steps.

Difference between two lists:

Steps to solve the program
  1. Take two lists as input.
  2. Print the elements from the list1 who are not in list2.
  3. Use for loop and an if statement for this purpose.
				
					#Input list
list1 = [34,55,12,89,44,67]
list2 = [55,89,34]

for value in list1:
    if value not in list2:
    #Printing output
        print(value, end=" ")
				
			

Output :

				
					12 44 67
				
			

Related Articles

Merge all elements using special character

Reverse each element of the list

31. Problem to merge all elements using a special character

In this program, we will take a user input as a list and merge all elements of the list in a single entity using a special character with the help of the below-given steps.

Merge all elements in a list:

Steps to solve the program
  1. Take a list of characters as input.
  2. Join the characters from the list using join() to merge all elements.
  3. Use “$” to join the character.
  4. Print the output.
				
					#Input list
list1 = ["a","b","c","d"]

#Joining and printing output
print("$".join(list1))
				
			

Output :

				
					a$b$c$d
				
			

Related Articles

Find the second smallest number from the list

Get the difference between two lists

30. Problem to find the second smallest number from the list.

In this program, we will take a user input as a list. Find the second smallest number from the list with the help of the below-given steps.

Second smallest number in a list:

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the second smallest number from the list using indexing.
				
					#Input list
list1 = [22,11,78,45,90,44]

#Sorting the list
list1.sort()

#Printing output
print("2nd smallest number: ", list1[1])
				
			

Output :

				
					2nd smallest number:  22
				
			

Related Articles

Find the second largest number from the list

Merge all elements using special character

29. Problem to find the second largest number in the list.  

In this program, we will take a user input as a list. Find the second largest number from the list with the help of the below-given steps.

Second largest number in a list:

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the second largest number from the list using indexing.
				
					#Input list
list1 = [22,11,78,45,90,44]

#sorting the list
list1.sort()

#printing the output
print("2nd largest number: ", list1[-2])
				
			

Output :

				
					2nd largest number:  78
				
			

Related Articles

Generate all sublists with 5 or more elements

Find the second smallest number from the list

28. Problem to generate all sublists with 5 or more elements.

In this program, we will take a user input as a list. Generate all sublists having 5 or more elements in it from the given list with the help of the below-given steps.

Generate all sublists of a list:

Steps to solve the program
  1. Take a list as input and create an empty list to store and generate all sublists.
  2. From itertools import combinations for generating sublists with 5 or more elements.
  3. Use for loop and range function for this purpose.
  4. If the generated list contains 5 or more elements add that list as a sublist to the empty list.
  5. Print the list to see the output.
				
					#Importing combinations
from itertools import combinations

#Input list
list1 = [1,3,5,8,0,22,45]

#Creating an empty list
sub = []

for i in range(0,len(list1)+1):
#Checking for combinations
    for sublist in combinations(list1,i):
        temp = list(sublist)
        if len(temp) > 4:
            sub.append(temp)

#Printing output
print(sub)
				
			

Output :

				
					[[1, 3, 5, 8, 0],
 [1, 3, 5, 8, 22],
 [1, 3, 5, 8, 45],
 [1, 3, 5, 0, 22],
 [1, 3, 5, 0, 45],
 [1, 3, 5, 22, 45],
 [1, 3, 8, 0, 22],
 [1, 3, 8, 0, 45],
 [1, 3, 8, 22, 45],
 [1, 3, 0, 22, 45],
 [1, 5, 8, 0, 22],
 [1, 5, 8, 0, 45],
 [1, 5, 8, 22, 45],
 [1, 5, 0, 22, 45],
 [1, 8, 0, 22, 45],
 [3, 5, 8, 0, 22],
 [3, 5, 8, 0, 45],
 [3, 5, 8, 22, 45],
 [3, 5, 0, 22, 45],
 [3, 8, 0, 22, 45],
 [5, 8, 0, 22, 45],
 [1, 3, 5, 8, 0, 22],
 [1, 3, 5, 8, 0, 45],
 [1, 3, 5, 8, 22, 45],
 [1, 3, 5, 0, 22, 45],
 [1, 3, 8, 0, 22, 45],
 [1, 5, 8, 0, 22, 45],
 [3, 5, 8, 0, 22, 45],
 [1, 3, 5, 8, 0, 22, 45]]
				
			

Related Articles

Check whether a list contains a sublist

Find the second largest number from the list

27. Problem to check whether a list contains a sublist or not.

In this program, we will take two lists as inputs and check whether the list contains a sublist with the help of the below-given steps.

List contains a sublist:

Steps to solve the program
  1. Take two lists as input.
  2. Check whether list2 is a sublist of list1 (i.e. all the elements from the list2 are in list1.
  3. Use for loop for this purpose.
  4. Print the output.
				
					#Input list
list1 = [22,45,67,12,78]

#Sublist
list2 = [45,12]

#Creating count variable
count = 0

for value in list1:
    for element in list2:
        if value == element:
            count += 1
            
#Printing output
if count == len(list2):
    print("Sublist exists")
else:
    print("Sublist does not exists")
				
			

Output :

				
					Sublist exists
				
			

Related Articles

Find the max, min, and sum of the list

Generate all sublists with 5 or more elements

26. Problem to find the max min and sum of the list.

In this program, we will take a user input as a list. Find the max min and sum of the list using in-built functions. Print the final output with the help of the below-given steps.

Max Min and Sum of the list elements:

Steps to solve the program
  1. Take a list as input.
  2. Find the minimum element from the list using min().
  3. Find the maximum element from the list using max().
  4. Find the sum of the list using sum().
  5. Print the output.
				
					#Input list
list1 = [22,66,89,9,44]

#Printing output
print("Minimum: ", min(list1))
print("Maximum: ", max(list1))
print("Sum: ", sum(list1))
				
			

Output :

				
					Minimum:  9
Maximum:  89
Sum:  230
				
			

Related Articles

Remove data from the list using pop method

Check whether a list contains a sublist

25. Problem to remove element from the list using pop method.

In this program, we will take a user input as a list. Remove the from the list from a specific index using pop method. Print the final output with the help of the below-given steps.

Remove element from the list using pop method:

Steps to solve the program
  1. Take a list as input.
  2. Remove an element whose index is 2 using pop method.
  3. Print the output.
				
					#Input list
list1 = [33,56,89,12,45]

#Removing elements which is at
#2nd index and printing output
list1.pop(2)
				
			

Output :

				
					[33, 56, 12, 45]
				
			

Related Articles

Sort a list using the sort and sorted method

Find the max, min, and sum of the list

24. Problem to sort a list using the sort and sorted method.

In this program, we will take a user input as a list. Sort the given list using the sort and sorted method with the help of the below-given steps.

Table of Contents

Sort and sorted method:

Method 1 : Using Sort()

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the output.
				
					#Input list
list1 = [23,11,78,45,33]

#Using sort
list1.sort()
print("By sort function: ",list1)
				
			

Output :

				
					By sort function:  [11, 23, 33, 45, 78]

				
			

Method 2 : Using sorted()

Steps to solve the program
  1. Take a list as input,
  2. Sort the list using sorted().
  3. Print the output.
				
					#Input list
list1 = [23,11,78,45,33]

#using sorted
print("By sorted method: ",sorted(list1))
				
			

Output :

				
					By sorted method:  [11, 23, 33, 45, 78]
				
			

Related Articles

Add two lists using extend method

Remove data from the list using pop method

23. Problem to add two lists using extend method.

In this program, we will take two lists as inputs and combine them to make a single list using extend method with the help of the below-given steps.

Add lists using extend method:

Steps to solve the program
  1. Take two lists as input.
  2. Add them using extend().
  3. Print the output.
				
					#Input lists
list1 = [2,4,6,8,1]
list2 = [23,56,11,89]

#Combining lists
list1.extend(list2)

#Printing output
print(list1)
				
			

Output :

				
					[2, 4, 6, 8, 1, 23, 56, 11, 89]
				
			

Related Articles

List of words which has vowels in string

Sort a list using the sort and sorted method