10. Problem to split the list into two-part.

In this program, we will take a user input as a list and split the list into two-part, the left side all odd values and the right side all even values i.e. split the list with the help of the below-given steps.

Split the list:

Steps to solve the program
  1. Take a list as input and create two empty lists.
  2. Add even and odd elements from the given list into the empty list with the help of for loop.
  3. Combine odd element list with even element list using extend().
  4. Print the output.
				
					#Input list
list1 = [5, 7, 2, 8, 11, 12, 17, 19, 22]

#Creating empty list
even = []
odd = []

for value in list1:
    if value%2 == 0:
        even.append(value)
    else:
        odd.append(value)

#Combining lists
odd.extend(even)

#printing output
print(odd)
				
			

Output :

				
					[5, 7, 11, 17, 19, 2, 8, 12, 22]
				
			

Related Articles

Squares of all even numbers

Get common elements from two lists.

9. Problem to print squares of even numbers from a list.

In this program, we will take a user input as a list and print squares of even numbers from that list with the help of the below-given steps.

Squares of even numbers:

Steps to solve the program
  1. Take a list as input.
  2. Using for loop check whether an element from the list is even or not.
  3. If an element is even then print its square using **n.
				
					#Input list
list1 = [2,4,7,8,5,1,6]

for value in list1:
    if value%2 == 0:
        #Printing output
        print(value,":",value**2)
				
			

Output :

				
					2 : 4
4 : 16
8 : 64
6 : 36
				
			

Related Articles

Combination of 2 elements from the list whose sum is 10.

split the list with even,odd values

8. Problem to find the combination of 2 elements from the list.

In this program, we will take a user input as a list and print the combination of 2 elements whose sum is 10 with the help of the below-given steps.

Combination of 2 elements:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Import itertools.
  3. With the help of for loop and itertools.combinations check for combinations from the given list whose sum is 10.
  4. Add such combinations to the empty list.
  5. Print the list to see the combination of 2 elements.
				
					#Importing itertools
import itertools

#Input list
list1 = [2,5,8,5,1,9]

#Creating variable
var = 10

#Creating empty list
list3 = []

for i in range(len(list1)):
    for combi in itertools.combinations(list1,i):
        if sum(combi) == var:
            list3.append(combi)

#Printing output
print(list3)
				
			

Output :

				
					[(2, 8), (5, 5), (1, 9)]
				
			

Related Articles

Remove all duplicate elements

Squares of all even numbers

7. Problem to remove all the duplicate elements from a list.

In this program, we will take a user input as a list & remove all the duplicate elements with the help of the below-given steps.

Remove duplicates from list:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. With the help of for loop add elements from the given list to the empty list.
  3. If an element repeats then do not add it.
  4. Print the list to see the output.
				
					#Input list
list1 = [5,7,8,2,5,0,7,2]
 
#Creating empty list
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)
        
#Printing Output
print(list2)

				
			

Output :

				
					[5, 7, 8, 2, 0]
				
			

Related Articles

Differentiate even and odd elements

Combination of 2 elements from the list whose sum is 10.

6. Problem to differentiate even and odd elements from a list.

In this program, we will take a user input as a list and differentiate even and odd elements with the help of the below-given steps.

Differentiate even and odd number in list:

Steps to solve the program
  1. Take a list as input and create two empty lists with names odd and even.
  2. Use for loop to iterate over each element of the given list.
  3. Use the if statement to check whether the element is even or not during iteration.
  4. If even add that element to the even list, if not add it to the odd list to differentiate even and odd numbers.
  5. Print both lists to see the output.
				
					#Input list
og_list = [23,11,78,90,34,55]

#creating empty lists
odd = []
even = []

for value in og_list:
    if value%2 == 0:
        even.append(value)
    else:
        odd.append(value)
        
#printing output
print("Even numbers: ", even)
print("Odd numbers: ", odd)
				
			

Output :

				
					Even numbers:  [78, 90, 34]
Odd numbers:  [23, 11, 55]
				
			

Related Articles

minimum and maximum elements

Remove all duplicate elements

5. Problem to find minimum and maximum elements from a list

In this program, we will take a user input as a list and find the minimum and maximum elements from it with the help of the below-given steps.

Table of Contents

Minimum and maximum element in list:

Method 1 : Sorting and indexing

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using Sort().
  3. Print the maximum value i.e. element with -1 index.
  4. Print the minimum value i.e. element with 0 index.
				
					#Input list
list1 = [23,56,12,89]

#Sorting list
list1.sort()

#Printing output
print("Smallest number: ", list1[0])
print("Largest number: ", list1[-1])
				
			

Output :

				
					Smallest number:  12
Largest number:  89
				
			

Minimum and maximum element in list:

Method 2 : Using in-built function

Steps to solve the program
  1. Take a list as input.
  2. Print the maximum value using max().
  3. Print the minimum value using min().
				
					#Input list
list1 = [23,56,12,89]

#Printing output
print("Smallest number: ", min(list1))
print("Largest number: ", max(list1))
				
			

Output :

				
					Smallest number:  12
Largest number:  89
				
			

Related Articles

Product of all elements

 Differentiate even and odd elements

4. Problem to get product of all elements in a list

In this program, we will take a user input as a list & iterate all the values one by one with the python loop to find the product of all elements in a list with the help of the below-given steps.

Table of Contents

Method 1:  Using for loop

Steps to solve the program
  1. Take a list as input.
  2. Using for loop with the necessary condition find the product of all elements in a list.
				
					#Input list
list1 = [3,6,9,2]

#Creating variable
var = 1

for value in list1:
    var *= value
    
#Printing output
print(var)
				
			

Output :

				
					324
				
			

Method 2 : Using while loop

Steps to solve the program
  1. Take a list as input.
  2. Use while loop with the necessary conditions to find the product of all elements of the list.
				
					#Input list
list1 = [3,6,9,2]

#creating variables
product = 1
count = 0

while count < len(list1):
    product *= list1[count]
    count += 1

#Printing output
print(product)
				
			

Output :

				
					324
				
			

Related Articles

Sum of all elements

Minimum and maximum elements

3. Problem to print the sum of all elements in a list.

In this program, we will take a user input as a list & iterate all the values one by one with the python loop to find the sum of all elements in a list.

Table of Contents

Method 1: Using for loop

Steps to solve the program
  1. Take a list as input.
  2. Use for loop to iterate over every element of the list.
  3. Find and print the sum of all elements of the list using the appropriate condition.
				
					#Input list
list1 = [2,5,8,0,1]

#Creating a variable
var = 0

for value in list1:
    var += value

print(var)
				
			

Output :

				
					
16
				
			

Method 2 : Using while loop

Steps to solve the program
  1. Take a list as input.
  2. Using while loop and necessary condition find and print the sum of all elements of the list.
				
					#Input list
list1 = [2,5,8,0,1]

#Creating variable
count = 0
total = 0

while count < len(list1):
    total += list1[count]
    count += 1

#Printing output
print(total)
				
			

Output :

				
					16
				
			

Method 3 : Using sum() function

Steps to solve the program
  1. Take a list as input.
  2. Use sum() function to find the sum of all the elements of the list.
				
					#Input list
list1 = [2,5,8,0,1]

#Printing output
print(sum(list1))
				
			

Output :

				
					16
				
			

Related Articles

 Combine two lists

Product of all elements

2. Problem to add elements from list1 to list2.

There are two ways to add elements from list1 to list2, fone is concatenation with the plus operator and the second one is to add elements with extend method with the help of the below-given steps.

Table of Contents

Method 1 : Using concatenation

Add elements:

Steps to solve the program
  1. Take two lists as inputs.
  2. Add them using the ” ” operator.
  3. Print the final list.
				
					#Input lists
list1 = [3, 6, 7, 81, 2]
list2 = [11, 15, 17, 13]

#Adding lists
output = list1 + list2

#Printing output
print(output)

				
			

Output :

				
					[3, 6, 7, 81, 2, 11, 15, 17, 13]

				
			

Method 2 : Using extend()

Steps to solve the program
  1. Take two lists as input.
  2. Combine both lists using extend().
  3. Print the combined list.
				
					#Input lists
list1 = [3, 6, 7, 81, 2]
list2 = [11, 15, 17, 13]

#Extend method
list1.extend(list2)

#Printing output
print(list1)

				
			

Output :

				
					[3, 6, 7, 81, 2, 11, 15, 17, 13]
				
			

Related Articles

Square of each number

Sum of all list elements.

1. Problem to get the square of all numbers In the python list

In this program, we will take a user input as a list and print the square of all numbers In the Python list with the help of the below-given steps.

Table of Contents

Method 1: Using for loop.

Square of all numbers in list:

Steps to solve the program
  1. Take a list as input.
  2. Using for loop and **n print the square of each element from the list.
				
					list1 = [3, 5, 7, 1, 8]

for val in list1:
    print(val, ":", val**2)
				
			

Output :

				
					
3 : 9
5 : 25
7 : 49
1 : 1
8 : 64
				
			

Method 2 : Using while loop.

Square of all numbers in list:

Steps to solve the program
  1. Take a list as input.
  2. Using while loop and necessary condition print the square of each element from the list.
				
					#input list 
list1 =[3,5,7,1,8]

#Creating count variable
count = 0

while count < len(list1):
    print(list1[count],":",list1[count]**2)
    count += 1
				
			

Output :

				
					3 : 9
5 : 25
7 : 49
1 : 1
8 : 64
				
			

Related Articles

Add elements from list1 to list2