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.

Leave a Comment