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

61. Problem to convert characters to Uppercase and lowercase in a list

In this Python list program, we will take a user input as a list and the first and last letter of each item from Uppercase and Lowercase with the help of the below-given steps.

Convert characters to Uppercase and lowercase in a list:

Steps to solve the program
  1. Take a list with words as input.
  2. Create two empty lists.
  3. Use a for loop to iterate over words in the list.
  4. Convert the first and last character of each word into an Uppercase and Lowercase character and add them to the corresponding empty lists using the upper() and lower() function and indexing.
  5. Print the lists to see the output.
				
					#Input list
list1 = ["Learn", "python", "From", "Sqa", "tools"]

#Creating empty lists
list2 = []
list3 = []
for words in list1:
    list2.append( words[0].upper()+words[1:-1]
                 + words[-1].upper() + " ")
#Printing output
print("Uppercase: ",list2)

for words in list1:
    list3.append( words[0].lower()+words[1:-1] 
                 + words[-1].lower() + " ")
#Printing output    
print("Lowercase: ",list3)
				
			

Output :

				
					Uppercase:  ['LearN ', 'PythoN ', 'FroM ', 'SqA ', 'ToolS ']
Lowercase:  ['learn ', 'python ', 'from ', 'sqa ', 'tools ']
				
			

Related Articles

Zip two lists of lists into a list

Maximum and minimum from the heterogeneous list

60. Problem to zip two lists of lists into a list.

In this Python list program, we will take two lists of lists as input and zip two lists of lists into a list with the help of the below-given steps.

Zip two lists:

Steps to solve the program
  1. Take two lists of lists as input.
  2. Combine / zip two lists inside a new list using the zip() function.
  3. Print the list to see the output.
				
					#Input list
list1 = [[1, 3], [5, 7], [9, 11]]
list2 = [[2, 4], [6, 8], [10, 12, 14]]

#Creating new list
list3 = list(zip(list1,list2))

#Printing output
print(list3)
				
			

Output :

				
					[([1, 3], [2, 4]), ([5, 7], [6, 8]), ([9, 11], [10, 12, 14])]
				
			

Related Articles

Create a 3*3 grid with numbers

Character to Uppercase and lowercase

59. Problem to create a 3×3 grid with numbers.

In this Python list program, we will create a 3*3 grid with numbers in a list using the Python programming language.

Create a 3×3 grid with numbers in list:

Steps to solve the program
  1. Create an empty list.
  2. Use a for loop to iterate over numbers from 0 to 2.
  3. In each iteration add an empty list to the created empty list.
  4. Use a nested for loop to iterate over numbers from 4 to 6.
  5. Add those numbers to the empty that we have created in the previous for loop.
  6. Print the list to see the output i.e. 3×3 grid with numbers
				
					#Input list
list1 = []

for i in range(3):
    list1.append([])
    for j in range(4,7):
        list1[i].append(j)
        
#Printing output
print("3X3 grid: ",list1)
				
			

Output :

				
					3X3 grid:  [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
				
			

Related Articles

Select random numbers from the list

Zip two lists of lists into a list

58. Problem to select random numbers from the list.

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

Select random numbers from the list:

Steps to solve the program
  1. Take a list as input.
  2. Create an empty list and a variable.
  3. Import a random library to select random numbers from the list.
  4. Add those randomly generated numbers to the empty list.
  5. Print the list two see the output.
				
					#Input list
list1 = [1, 4, 5, 7, 3, 2, 9]

#Creating an empty list
list2 = []
count=0

#Importing random library
import random

while count<4:
    list2.append(random.choice(list1))
    count += 1

#Printing output
print(list2)
				
			

Output :

				
					[7, 7, 4, 3]
				
			

Related Articles

Insert items in the list at a specific location

Create a 3*3 grid with numbers