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

55. Problem to pack consecutive duplicates of given list.

In this Python list program, we will take a user input as a list and pack consecutive duplicates of given list elements into sublists with the help of the below-given steps.

Pack consecutive duplicates in the list:

Steps to solve the program
  1. Take a list of consecutive duplicate elements as input.
  2. Import groupby from itertools.
  3. Create a new list using list comprehension to pack consecutive duplicates elements into sublists using groupby.
  4. Print the list to see the output.
				
					#Input list
list1 = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]
from itertools import groupby

#Creating new list
list2 = [list(group) for key, group in groupby(list1)]

#Printing output
print(list2)
				
			

Output :

				
					[[0, 0], [1], [2], [3], [4, 4], [5], [6, 6], [7], [8, 8], [9]]
				
			

Related Articles

Remove consecutive duplicates of given lists

Split a given list into two parts

54. Problem to remove consecutive duplicates of given lists.

In this Python list program, we will take a user input as a list and remove consecutive duplicates of given lists with the help of the bewlo-given steps.

Remove consecutive duplicates in a list:

Steps to solve the program
  1. Take a list having consecutive duplicate elements as input.
  2. Create an empty list to store the elements after we remove consecutive duplicates in the list.
  3. Import groupby from itertools to groupby elements from the given list.
  4. Add an element with 0 indexes after applying groupby to each element to the empty list after each iteration.
  5. Print the list to see the output.
				
					#Input list
list1 = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]

#Creating empty list
list2 = []

from itertools import groupby
for value in groupby(list1):
    list2.append(value[0])

#Printing output    
print(list2)
				
			

Output :

				
					[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4]
				
			

Related Articles

Count empty dictionaries from the given list

Pack consecutive duplicates of given list

53. Problem to count empty dictionaries from the given list.

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

Count empty dictionaries in a list:

Steps to solve the program
  1. Take user input as list containing dictionaries, lists, and tuples.
  2. Create an empty list.
  3. Use for loop to iterate over the given list.
  4. Use isinstance function to check whether the element in the list is a dictionary or not if yes and that dictionary is empty add that dictionary to the empty list.
  5. Count the number of empty dictionaries.
  6. Print the result to see the output.
				
					#Input list
list1 = [{}, {"a": "sqatools"}, [], {"a": 123}, {}, {}, ()]

#Empty list
list2 = []
for element in list1:
#Checking for dictionary
    if isinstance(element, dict):
        if len(element) == 0:
            list2.append(element)

#Printing output            
print(len(list2))
				
			

Output :

				
					3
				
			

Related Articles

Items that start with a specific character

Remove consecutive duplicates of given lists

52. Problem to get items that start with a specific character in a list

In this Python list program, we will take a user input as a list and find the items that start with a specific character from a given list with the help of the below-given steps.

List items that start with specific character:

Steps to solve the program
  1. Take a list as input containing strings starting with required characters.
  2. Create empty lists according to the requirements to store the items that start with a specific character.
  3. Add only those strings starting with the given characters in those empty strings.
  4. Use for loop and nested if-else statements for this purpose.
  5. Print the lists to see the result.
				
					#Input list
list1 = ["abbcd", "ppq", "abd", "agr", "bhr", "sqqa", "tools", "bgr"]

#Creating empty list
with_a = []
with_b = []
with_c = [] 

for word in list1:
    if word[0] == "a":
        with_a.append(word)
    elif word[0] == "b":
        with_b.append(word)
    elif word[0] == "c":
        with_c.append(word)

#Printing output
print("Starting with a: ", with_a)
print("Starting with b: ", with_b)
print("Starting with c: ", with_c)
				
			

Output :

				
					Starting with a:  ['abbcd', 'abd', 'agr']
Starting with b:  ['bhr', 'bgr']
Starting with c:  []
				
			

Related Articles

List of lists whose sum of elements is highest

Count empty dictionaries from the given list.

51. Problem to create a list of lists whose sum of elements is highest.

In this Python list program, we will take a user input as a list and find the list in a list of lists whose sum of elements is the highest with the help of the below-given steps.

List of lists having sum of list elements:

Steps to solve the program
  1. Take user input as a list of lists.
  2. Use the built-in max() function and set key=sum to get the list having the maximum sum of elements.
  3. Print that list.
				
					#Input list
list1 = [[11, 2, 3], [4, 15, 2], [10, 11, 12], [7, 8, 19]]

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

Output :

				
					[7, 8, 19]
				
			

Related Articles

Move all zero digits to the end

Items that start with a specific character

50. Problem to move all zero digits to the end.

In this program, we will take a user input as a list and move all zero digits to the end of a given list of numbers with the help of the below-given steps.

Move all zero digits to the end:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable var and assign its value equal to 0.
  3. Use for loop with the range function to iterate over elements in the list.
  4. If the iterated element is greater than 0 then assign its value to the temp variable.
  5. Assign the value of that index number equal to list1[var]
  6. Now change the value of list1[var] to temp variable.
  7. Add 1 to the var variable.
  8. Print the output to see the result i.e. move all zero digits to the end.
				
					#Input list
list1 = [3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]
var = 0

for i in range(0,len(list1)) :
    if (list1[i] > 0) :
        temp = list1[i]
        list1[i] = list1[var]
        list1[var]= temp
        var += 1

#Printing output
print(list1)
				
			

Output :

				
					[3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0]
				
			

Related Articles

Move all positive numbers on the left

List of lists whose sum of elements is highest

49. Problem to move all positive numbers on the left

In this Python list program, we will take a user input as a list and move all positive numbers to the left side and negative numbers to the right side with the help of the below-given steps.

Move all positive numbers in a list:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable var and assign its value equal to 0.
  3. Use for loop with the range function to iterate over elements in the list.
  4. If the iterated element is greater than 0 then assign its value to the temp variable.
  5. Assign the value of that index number equal to list1[var]
  6. Now change the value of list1[var] to temp variable.
  7. Add 1 to the var variable.
  8. Print the output to see the result i.e. move all positive numbers to the left.
				
					#Input list
list1 = [2, -4, 6, 44, -7, 8, -1, -10]
var = 0

#Sorting list
for i in range(0,len(list1)) :
    if (list1[i] > 0) :
        temp = list1[i]
        list1[i] = list1[var]
        list1[var]= temp
        var += 1

#Printing output        
print(list1)
				
			

Output :

				
					[2, 6, 44, 8, -7, -4, -1, -10]
				
			

Related Articles

Iterate over lists and create a list of sublists

Move all zero digits to the end

48. Problem to create a list of sublists.

In this Python list program, we will take two lists as input and iterate over lists to create a list of sublists with the help of the below-given steps.

Create a list of sublists:

Steps to solve the program
  1. Take two lists as inputs.
  2. Create an empty list to store a list of sublists.
  3. Use for loop and zip function to combine the input lists and add them to the empty list.
  4. Print the list to see the output.
				
					#Input lists
list1 = [1, 3, 5, 7, 9]
list2 = [8, 6, 4, 2, 10]

#Creating empty list
empty_list = []
for (a,b) in zip(list1,list2):
    elements = [a, b]
    empty_list.append(elements)

#printing output    
empty_list
				
			

Output :

				
					[[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
				
			

Related Articles

Insert a given string at the beginning

Move all positive numbers on the left