77. Problem to calculate the factorial of each item in the list.

In this Python list program, we will calculate the factorial of each item in the list with the help of the below-given steps.

Factorial of each item in list:

Steps to solve the program
  1. Take a list as input.
  2. Create a function for calculate factorial of a number.
  3. Create a new list and calculate the factorial of each number from the given list using list comprehension.
  4. Print the new list to see the output.
				
					#Creating function
def factorial(n):
    fact = 1
    
    for i in range(1, n + 1):
        fact *= i
    return fact

#Input list
list1 = [1, 2, 3, 4]
list1_factorials = [factorial(value) for value in list1]

#Printing output
list1_factorials
				
			

Output :

				
					[1, 2, 6, 24]
				
			

Related Articles

Remove the duplicate string from the list

Get a list of Fibonacci numbers from 1 to 20

76. Problem to remove the duplicate string from the list.

In this Python list program, we will take a user input as a list and remove the duplicate string from the list with the help of the below-given steps.

Remove the duplicate string from list:

Steps to solve the program
  1. Take a list containing strings as input.
  2. Add the strings from the given list to another list using for loop. 
  3. If a string repeats then do not add it.
  4. Print the list to see the output.
				
					#Input lists
list1 = ["python", "is", "a", "best", "language", "python", "best"]
list2 = []

for words in list1:
    if words not in list2:
        list2.append(words)

#Printing output
print(list2)
				
			

Output :

				
					['python', 'is', 'a', 'best', 'language']
				
			

Related Articles

Insert an element before each element of a list

Calculate the factorial of each item in the list

75. Problem to insert an element before each element of a list.

In this Python list program, we will take a user input as a list and insert an element before each element of a list with the help of the below-given steps.

Insert an element before each element in list:

Steps to solve the program
  1. Take a list as input.
  2. Add ‘a’ before each element of the list using list comprehension.
  3. Print the output to see the result.
				
					#Input list
list1 = [3, 5, 7, 8]
list1 = [text for value in list1 for 
         text in ('a', value)]

#Printing output
print(list1)
				
			

Output :

				
					['a', 3, 'a', 5, 'a', 7, 'a', 8]
				
			

Related Articles

Remove duplicate tuples from the list

Remove the duplicate string from the list

74. Problem to remove duplicate tuples from the list.

In this Python list program, we will take a user input as a list and remove duplicate tuples from the list with the help of the below-given steps.

Remove duplicate tuples from list:

Steps to solve the program
  1. Take a list of tuples as input.
  2. Using for loop add the tuples from the given list to another list.
  3. If a tuple repeats then do not add it.
  4. Print the output to see the result.
				
					#Input list
list1 = [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
list2 = []

for value in list2:
    if value not in list2:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[(2, 3), (4, 6), (5, 1), (7, 9)]
				
			

Related Articles

Create a list by taking an alternate item.

Insert an element before each element of a list

73. Problem to create a list by taking an alternate item.

In this Python list program, we will take a user input as a list and create a new list by taking an alternate item from the list with the help of the below-given steps.

Create a list with alternate items:

Steps to solve the program
  1. Take a list as input.
  2. Create a new list that will contain alternate elements from the given list.
  3. Use for loop with range function for this purpose.
  4. Print the new list to see the output.
				
					#Input lists
list1 = [3, 5, 7, 8, 2, 9, 3, 5, 11]
list2 = []

for i in range(0,len(list1),2):
    list2.append(list1[i])

#Prinitng output
print(list2)
				
			

Output :

				
					[3, 7, 2, 3, 11]
				
			

Related Articles

Remove duplicate sublists from the list

Remove duplicate tuples from the list

72. Problem to remove duplicate sublists from the list.

In this Python list program, we will take a user input as a list and remove duplicate sublists from the list with the help of the below-given steps.

Remove duplicate sublists:

Steps to solve the program
  1. Take a list of sublists as input.
  2. Add the sublists from the given list to another list.
  3. Use for loop for this purpose.
  4. If a sublist repeats do not add it to the other list use an iif statement for this purpose.
  5. Print the list to see the output.
				
					#Input list
list1 = [[1, 2], [3, 5], [1, 2], [6, 7]]
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[[1, 2], [3, 5], [6, 7]]
				
			

Related Articles

Elements are unique or not from the given list

Create a list by taking an alternate item

71. Problem to check elements are unique or not in the given list.

In this Python list program, we will take a user input as a list and check if the elements are unique or not in the given list with the help of the below-given steps.

List elements are unique:

Steps to solve the program
  1. Take a list as input.
  2. Check whether all elements are unique or not from the given list using for loop and if statement.
  3. If even one element repeats in the list break the loop.
  4. If they are unique print True otherwise False.
				
					#Input list
list1 = [2, 5, 6, 7, 4, 11, 2, 4, 66, 21, 22, 3]

for i in range(len(list1)):
    if list1[i] not in list1[i+1:]:
        print("True")
        break
    else:
        print("False")
        break
        
list1 = [2, 5, 8, 3, 6, 21]

for i in range(len(list1)):
    if list1[i] not in list1[i+1:]:
        print("True")
        break
    else:
        print("False")
        break
				
			

Output :

				
					False

True
				
			

Related Articles

Remove duplicate dictionaries from a given list

Remove duplicate sublists from the list

70. Problem to remove duplicate dictionaries from a list.

In this Python list program, we will take a user input as a list and remove duplicate dictionaries from a given list with the help of the below-given steps.

Remove duplicate dictionaries:

Steps to solve the program
  1. Take a list of dictionaries as input.
  2. Create an empty list.
  3. Check whether each element in the given list is unique or not using for loop and an if statement.
  4. If it is unique then add it to the empty list.
  5. Print the list to see the output i.e. list after we remove duplicate dictionaries from it.
				
					#Input lists
list1 = [{"name": "john"}, {"city": "mumbai"}, {"Python": "laguage"}, {"name": "john"}]
list2 = []

for dic in list1:
    if dic not in list2:
        list2.append(dic)

#Printing output
print(list2)
				
			

Output :

				
					[{'name': 'john'}, {'city': 'mumbai'}, {'Python': 'laguage'}]
				
			

Related Articles

Check whether a specified list is sorted or not

Elements are unique or not from the given list

69. Problem to check if a list is sorted or not.

In this Python list program, we will take a user input as a list and check whether a specified list is sorted or not with the help of the below-given steps.

Check if a list is sorted:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable and assign the given list in reverse order to it.
  3. Sort the given list.
  4. Check if both lists are similar i.e. the list is sorted or not using an if-else statement.
  5. If yes print True otherwise print False
				
					#Input lists
list1 = [1, 2, 3, 5, 7, 8, 9]
list2 = list1

#Sorting list
list1.sort()

#Printing output
if list1 == list2:
    print(True)
else:
    print(False)
    
list1 = [3, 5, 1, 6, 8, 2, 4]
list2 = list1
list1.sort()
if list1 == list2:
    print(True)
else:
    print(False)
				
			

Output :

				
					True

False
				
			

Related Articles

Access multiple elements of the specified index

Remove duplicate dictionaries from a given list

68. Problem to access multiple elements of the list

In this Python list program, we will take a user input as a list and access multiple elements of the specified index from a given list with the help of the below-given steps.

Access multiple elements of the list:

Steps to solve the program
  1. Take two lists as inputs one with values and the second one with index values. to access multiple elements from the given list.
  2. Create an empty list.
  3. Use a for loop to iterate over index numbers.
  4. Add multiple elements from the given list which is at the specified index in the empty list using the append() function.
  5. Print the list to see the output.
				
					#Input lists
list1 = [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]
index_list = [0, 3, 5, 6]
new_list = []

for value in index_list:
    new_list.append(list1[value])

#Printing output
print(new_list)
				
			

Output :

				
					[2, 7, 1, 5]
				
			

Related Articles

Count integers in a given mixed list

Check whether a specified list is sorted or not