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

57. Problem to insert items in the list at a specific location.

In this Python list program, we will take a user input as a list and insert items in the list at a specific location with the help of the below-given steps.

Insert items in the list:

Steps to solve the program
  1. Take a list as input.
  2. Insert an element at a specific location using the insert function.
  3. Print the output to see the result.
				
					#Input list
list1 = [2, 4, 6, 8, 3, 22]

#iserting element
list1.insert(3,55)

#Printing output
print(list1)
				
			

Output :

				
					[2, 4, 6, 55, 8, 3, 22]
				
			

Related Articles

Split a given list into two parts

Select random numbers from the list

56. Problem to split a given list into two parts.

In this Python list program, we will take a user input as a list and split a given list into two parts where the length of the first part of the list is given with the help of the below-given steps.

Split the list into two parts:

Steps to solve the program
  1. Take a list as input.
  2. Create an empty list.
  3. Split the given list into two parts where the length of the first part is given using indexing.
  4. Add those parts to the empty list.
  5. Print the list to see the output.
				
					#Input list
list1 = [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]
list2 = []

#length=4
list2.append(list1[:4])
list2.append(list1[4:])

#Printing output
print(list2)
				
			

Output :

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

Related Articles

Pack consecutive duplicates of given list

Insert items in the list at a specific location