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

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.