20. Problem to get a list of elements divided by a number

In this program, we will take a user input as a list & iterate all the elements one by one with the python loop. Print the list of elements divided by a number with the help of the below-given steps.

Elements divided by number:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Using for loop and if statement check whether an element from the given list is divisible by 3 or 7.
  3. Add only those elements to the empty list to get the list of elements divided by number.
  4. Print the list to see the output.
				
					#Input list
list1 = [3,7,0,2,6,14,88,21]

#Creating empty list
list2 = []

for value in list1:
#Checking whether the value is 
#divided by either 3 or 7
    if value%3 == 0 or value%7 == 0:
        list2.append(value)

#Printing output
print(value)
				
			

Output :

				
					[3, 7, 0, 6, 14, 21]
				
			

Related Articles

Remove negative elements from the list

Check whether the list is palindrome or not

19. Problem to remove negative elements from the list

We will take a user input as a list & iterate all the values one by one with the python loop to remove negative elements from it. Print only positive values with the help of the below-given steps.

Remove negative elements from list:

Steps to solve the program
  1. Take a list as input.
  2. Using for loop and if statement check whether an element from the given list is negative or positive.
  3. Print only positive elements.
				
					#Input list
list1 = [3,5,-8,0,-20,-55]

#Checking for positive values
for value in list1:
    if value >= 0:
        #Printing output
        print(value,end=" ")
				
			

Output :

				
					3 5 0
				
			

Related Articles

removing certain elements a list

List of elements divided by a number

18. Problem to print list after removing certain elements.

In this program, we will take a user input as a list & iterate all the values one by one with the python loop. Print the list after removing certain elements with the help of the below-given steps.

Removing certain elements from list:

Steps to solve the program
  1. Take a list as input.
  2. Using list comprehension, if statement and enumerate() remove certain elements from the list.
  3. Print the list after removing the elements.
				
					#Input list
list1 = [3,4,8,7,0,1,6,9]

list1 = [element for (value,element) in enumerate(list1) 
    if value not in (1,3,6)]

#Printing output
print(list1)
				
			

Output :

				
					[3, 8, 0, 1, 9]
				
			

Related Articles

common elements between lists

Remove negative elements

17. Problem to print true if common elements between lists.  

We will take two lists as input & iterate all the values one by one with the python loop with the help of the below-given steps. Print True if there are any common elements between lists.

Common elements between lists:

Steps to solve the program
  1. Take two lists as input.
  2. Use a for loop to iterate over elements in the first list.
  3. Using an If statement check whether an element exists in both lists or not.
  4. If yes print True.
				
					#Input list
list1 = [2,4,7,8,3]
list2 = [4,5,0]

for value in list1:
    if value in list2:
        #Printing output
        print("True")
				
			

Output :

				
					True
				
			

Related Articles

Copy the list to another list

removing certain elements from a list

16. Problem to copy the list to another list.

In this program, we will take a user input as a list & copy the list to another list with the help of the below-given steps.

Copy the list:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Add every element from the given list to the empty list using for loop.
  3. Print the list to see the output.
				
					#Input list
list1 = [1,2,4,7,0,5]

#Creating an empty list
list2 = []

for value in list1:
    list2.append(value)
    
#Printing output
print(list2)
				
			

Output :

				
					[1, 2, 4, 7, 0, 5]
				
			

Related Articles

Reverse a list with reversed and reverse methods

common elements between lists

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