w3resource

Python: Find the items start with specific character from a given list

Python List: Exercise - 70 with Solution

Write a Python program to find the items starts with specific character from a given list.

Sample Solution:-

Python Code:

def test(lst, char):
    result = [i for i in lst if i.startswith(char)]
    return result
text = ["abcd", "abc", "bcd", "bkie", "cder", "cdsw", "sdfsd", "dagfa", "acjd"]
print("\nOriginal list:")
print(text)
char = "a"
print("\nItems start with",char,"from the said list:")
print(test(text, char))
char = "d"
print("\nItems start with",char,"from the said list:")
print(test(text, char))
char = "w"
print("\nItems start with",char,"from the said list:")
print(test(text, char))

Sample Output:

Original list:
['abcd', 'abc', 'bcd', 'bkie', 'cder', 'cdsw', 'sdfsd', 'dagfa', 'acjd']

Items start with a from the said list:
['abcd', 'abc', 'acjd']

Items start with d from the said list:
['dagfa']

Items start with w from the said list:
[]

Flowchart:

Flowchart: Find the items start with specific character from a given list

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to remove duplicates from a list of lists.
Next: Write a Python program to check if all dictionaries in a list are empty or not.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Big Number Readability:

you can separate zeros with underscore (_)

print(2_000_000)
print(2_000_000 +1)

Output:

2000000
2000001