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

65. Problem to get the difference between consecutive numbers in list.

In this Python list program, we will take a user input as a list and find the difference between consecutive numbers in a given list with the help of the below-given steps.

Difference between consecutive numbers in list:

Steps to solve the program
  1. Take a list as input.
  2. Combine the elements from the list using the zip() function.
  3. Find the difference between the consecutive numbers in the list.
  4. Add that difference to a new list.
  5. Print the list to see the output.
				
					#Input list
list1 = [1, 1, 3, 4, 4, 5, 6, 7]

#Creating empty list
list2 = []

for a,b in zip(list1[:-1],list1[1:]):
    difference = b-a
    list2.append(difference)

#Printing output
print(list2)
				
			

Output :

				
					[0, 2, 1, 0, 1, 1, 1]
				
			

Related Articles

Extract strings of specified sizes from the list

Calculate the average of the list

64. Problem to extract strings of specified sizes from the list

In this Python list program, we will take a list containing strings as input and extract strings of specified sizes from a given list of string values with the help of the below-given steps.

Extract strings from list:

Steps to solve the program
  1. Take a list containing strings as input.
  2. Create an empty list.
  3. Use a for loop to iterate over strings in the list.
  4. If the length of the strings in the given list is more than the specified number than add those strings to the empty list.
  5. Print the list to see the output.
				
					#Input list
list1 = ["Python", "Sqatools", "Practice", "Program", "test", "lists"]

#Creating an empty string
list2 = []

for string in list1:
    if len(string)>  7:
        list2.append(string)

#Printing output
print(list2)
				
			

Output :

				
					['Sqatools', 'Practice']
				
			

Related Articles

Sort a given list by the sum of sublists

Difference between consecutive numbers in list

63. Problem to sort a list by the sum of sublists in Python.

In this Python list program, we will take a user input as a list of lists and sort a list by the sum of sublists with the help of the below-given steps.

Sort list by sum of sublists:

Steps to solve the program
  1. Take a list of lists as input.
  2. Sort the list by the sum of sublists using sorted() and set the key=sum.
  3. Print the output to see the result.
				
					#Input list
list1 = [[3, 5, 6], [2, 1, 3], [5, 1, 1], 
        [1, 2, 1], [0, 4, 1]]

#Printing output
print(sorted(list1, key=sum))
				
			

Output :

				
					[[1, 2, 1], [0, 4, 1], [2, 1, 3], [5, 1, 1], [3, 5, 6]]
				
			

Related Articles

Maximum and minimum from the heterogeneous list

Extract strings of specified sizes from the list

62. Problem to find maximum and minimum from the list.

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

Maximum and minimum from the list:

Steps to solve the program
  1. Take a heterogenous list as input.
  2. First check the element from the list is an integer or not using isinstance.
  3. If it is an integer then find the maximum and minimum from them using min() and max().
  4. Print the output to see the result.
				
					#Input list
list1 = ["Sqa", 6, 5, 2, "Tools"]

#Finding min and max
max_ = max(value for value in list1 if isinstance(value, int))
min_ = min(value for value in list1 if isinstance(value, int)) 

#Printing output
print("Maximun: ", max_)
print("Minimun: ", min_)
				
			

Output :

				
					Maximun:  6
Minimun:  2
				
			

Related Articles

Character to Uppercase and lowercase

Sort a given list by the sum of sublists