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

67. Problem to count integers in a given mixed list.

In this Python list program, we will take a user input as a list and count integers in a given mixed list with the help of the below-given steps.

Count integers in a list:

Steps to solve the program
  1. Take a list of mixed elements as input.
  2. Create a count variable to count integers in the given list.
  3. User for loop to iterate over each element of the list.
  4. Using isinstance() check whether the element is an integer or not.
  5. Print the final output to see the result.
				
					#Input list
list1 =["Hello", 45, "sqa",  23, 5, "Tools", 20]

#Crreating count variable
count = 0

for value in list1:
    if isinstance(value, int):
        count += 1

#Printing output        
print("Total number od integers: ",count)
				
			

Output :

				
					Total number of integers:  4
				
			

Related Articles

Calculate the average of the list

Access multiple elements of the specified index

66. Problem to calculate the average of the list.

In this Python list program, we will take a user input as a list and calculate the average of the list with the help of the below-given steps.

Average of the list elements:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable called total.
  3. Add each element of the list in the total variable using for loop.
  4. To find the average divide the total by length of the list.
  5. Print the output to see the result.
				
					#Input list
list1 = [3, 5, 7, 2, 6, 12, 3]

#Creating variable
total = 0

for value in list1:
    total += value
    
#Printing output
print("Average of list: ",total/len(list1))
				
			

output :

				
					Average of list:  5.428571428571429
				
			

Related Articles

Difference between consecutive numbers in list

Count integers in a given mixed list