15. Problem to reverse a list with reversed and reverse methods

In this program, we will take a user input as a list and reverse a list with reversed and reverse methods with the help of the below-given steps.

Table of Contents

Reverse a list:

Method 1 : Using reversed().

Steps to solve the program
  1. Take a list as input.
  2. Reverse a list using the reversed() function.
				
					#Input list
list1 = [2,3,7,9,3,1]

#Using reversed method
print("Using reversed: ",list(reversed(list1)))

				
			

Output :

				
					Using reversed:  [1, 3, 9, 7, 3, 2]
				
			

Method 2 : Using reverse()

Steps to solve the program
  1. Take a list as input.
  2. Print the given list in reverse order using reverse().
				
					#Input list
list1 = [2,3,7,9,3,1]

#using reverse method
list1.reverse()
print("Using reverse: ", list1)
				
			

Output :

				
					Using reverse:  [1, 3, 9, 7, 3, 2]
				
			

Related Articles

list in reverse order using index slicing.

Copy the list to another list.

14. Problem to print list in reverse order using index slicing.

In this program, we will take a user input as a list & print the list in reverse order using index slicing with the help of the below-given steps.

List in reverse order:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable and assign its value equal to the given list in the reverse order using indexing.
  3. Print the variable to see the output.
				
					#Input list
list1 = [2,4,6,8]

#Creating a variable
list2 = list1[::-1]

#Printing output
print(list2)
				
			

Output :

				
					[8, 6, 4, 2]
				
			

Related Articles

List in the reverse order using while loop

Reverse a list with reversed and reverse methods

13. Problem to print the list in reverse order using while loop

In this program, we will take a user input as a list & print the list in reverse order using a while loop with the help of the below-given steps.

List in reverse order:

Steps to solve the program
  1. Take a list as input.
  2. With the help of the While loop print the given list in the reverse order by using the necessary condition.
				
					#Input list
list1 = [1,2,3,4,5,6]

#Creating variable
count = len(a)-1

while count >= 0:
    #Printing output
    print(value[count], end=" ")
    count -= 1
				
			

Output :

				
					6 5 4 3 2 1 
				
			

Related Articles

List in the reverse order using for loop

List in reverse order using index slicing

12. Problem to print the list in reverse order using for loop

In this program, we will take a user input as a list & print the list in reverse order using for loop with the help of the below-given steps.

List in reverse order:

Steps to solve the program
  1. Take a list as input.
  2. Using for loop with range() function print the given list in the reverse order.
				
					#Input list
list1 = [1,2,3,4,55]

#Printing list in the reverse order
#Using for loop
for i in range(len(list1)-1,-1,-1):
    print(list1[i], end=" ")
				
			

Output :

				
					55 4 3 2 1
				
			

Related Articles

Common elements from two lists

List in the reverse order using while loop

11. Problem to get common elements from two lists.

In this program, we will take two lists as input and find the common elements between both lists with the help of the below-given steps.

Common elements from two lists:

Steps to solve the program
  1. Take 2 lists as input and create an empty list.
  2. Using for loop and if statement check whether an element from list1 exists in list2.
  3. If it exists then add it to the empty list.
  4. Print the list to see the common element.
				
					#Input list
list1 = [4, 5, 7, 9, 2, 1]
list2 = [2, 5, 8, 3, 4, 7]

#Creating empty list
common_list = []
for value in list1:
    if value in list2:
        common_list.append(value)

#Printing output
print(common_list)
				
			

Output :

				
					[4, 5, 7, 2]
				
			

Related Articles

split the list with even,odd values

List in reverse order using for loop

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