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
- Take a list as input containing strings starting with required characters.
- Create empty lists according to the requirements to store the items that start with a specific character.
- Add only those strings starting with the given characters in those empty strings.
- Use for loop and nested if-else statements for this purpose.
- 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
Python program to count empty dictionaries from the given list.
Python program to remove consecutive duplicates of given lists.
Python program to pack consecutive duplicates of given list elements into sublists.
Python program to insert items at a specific position in the list.
Python program to select random numbers from the list.