Get the first character of each string in a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Get the first character of each string in a List in Python
  2. Get first character of first string in a List in Python
  3. Get the first letter of each word in a String in Python

# Get the first character of each string in a List in Python

To get the first character of each string in a list:

  1. Use a list comprehension to iterate over the list.
  2. Access each string at index 0 and return the result.
  3. The new list will contain the first character of each list item.
main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [item[0] for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['b', 'h', 'c']

get first character of each string in list

The code for this article is available on GitHub

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the current item at index 0 and return the result

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The new list contains the first character of each string in the original list.

Alternatively, you can use a for loop.

# Get the first character of each string in a List using for loop

This is a three-step process:

  1. Declare a new variable that stores an empty list.
  2. Use a for loop to iterate over the original list.
  3. Append the first character of each string to the new list.
main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [] for item in my_list: new_list.append(item[0]) print(new_list) # ๐Ÿ‘‰๏ธ ['b', 'h', 'c']

get first character of each string in list using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the list.

On each iteration, we use the list.append() method to append the first character of the current string to the new list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

Which approach you pick is a matter of personal preference. I'd use a list comprehension as I find them quite direct and easy to read.

# Get first character of first string in a List in Python

Use two sets of square brackets to get the first character of the first string in a list.

The first set of square brackets returns the first element in the list and the second set returns the first character in the string.

main.py
my_list = ['bobby', 'hadz', 'com'] first = my_list[0][0] print(first) # ๐Ÿ‘‰๏ธ 'b' print(my_list[0][1]) # ๐Ÿ‘‰๏ธ 'o'

get first character of first string in list

The code for this article is available on GitHub
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The first set of square brackets is used to access the first item in the list.

main.py
my_list = ['bobby', 'hadz', 'com'] print(my_list[0]) # ๐Ÿ‘‰๏ธ bobby print(my_list[0][0]) # ๐Ÿ‘‰๏ธ b

The second set of square brackets is used to access the first character in the string.

# Handling a case where the list is empty or the first string is empty

If you try to access a list or a string at an index that doesn't exist, you'd get an IndexError exception.

You can use a try/except statement if you need to handle the exception.

main.py
my_list = ['', 'hadz', 'com'] try: print(my_list[0][0]) except IndexError: # ๐Ÿ‘‡๏ธ this runs print('The specified index is out of range.')

handle case where list is empty or first string is empty

The code for this article is available on GitHub
The first string in the list is empty, so trying to access it at index 0 raises an IndexError, which is then handled by the except block.

You can use the same approach to get the first N characters of the first string in a list.

main.py
my_list = ['bobby', 'hadz', 'com'] first = my_list[0][:1] print(first) # ๐Ÿ‘‰๏ธ 'b' first_two = my_list[0][:2] print(first_two) # ๐Ÿ‘‰๏ธ 'bo' first_three = my_list[0][:3] print(first_three) # ๐Ÿ‘‰๏ธ 'bob'

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

The slice my_str[:1] starts at index 0 and goes up to, but not including index 1, so it only selects the first character of the string.

When the start index is omitted, the slice starts at index 0.

If you need to get the first character of each string in the list, use a list comprehension.

main.py
my_list = ['bobby', 'hadz', 'com'] first_chars = [item[0] for item in my_list] print(first_chars) # ๐Ÿ‘‰๏ธ ['b', 'h', 'c']

We used a list comprehension to iterate over the list of strings.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the current string at index 0 and return the first character.

The new list contains the first character of each string in the list.

# Get the first letter of each word in a String in Python

To get the first letter of each word in a string:

  1. Use the str.split() method to split the string into a list of words.
  2. Use a list comprehension to iterate over the list.
  3. Access each word at index 0 and return the result.
main.py
my_str = 'my site is bobbyhadz.com' my_list = [word[0] for word in my_str.split()] print(my_list) # ๐Ÿ‘‰๏ธ ['m', 's', 'i', 'b']

get first letter of each word in string

The code for this article is available on GitHub

We used the str.split() method to split the string into a list of words.

The str.split() method splits the string into a list of substrings using a delimiter.

main.py
my_str = 'my site is bobbyhadz.com' # ๐Ÿ‘‡๏ธ ['my', 'site', 'is', 'bobbyhadz.com'] print(my_str.split())
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the current word at index 0 and return the result.

main.py
my_str = 'my site is bobbyhadz.com' my_list = [word[0] for word in my_str.split()] print(my_list) # ๐Ÿ‘‰๏ธ ['m', 's', 'i', 'b']
Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The new list contains the first letter of each word in the string.

Alternatively, you can use a for loop.

# Get the first letter of each word in a String using a for loop

This is a three-step process:

  1. Use the str.split() method to split the string into a list of words.
  2. Use a for loop to iterate over the list.
  3. Append the first letter of each word to a new list.
main.py
my_str = 'my site is bobbyhadz.com' my_list = [] for word in my_str.split(): my_list.append(word[0]) print(my_list) # ๐Ÿ‘‰๏ธ ['m', 's', 'i', 'b']

get first letter of each word in string using for loop

The code for this article is available on GitHub

We first used the str.split() method to split the string into a list of words and used a for loop to iterate over the list.

On each iteration, we append the first letter of the current word to a new list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

Which approach you pick is a matter of personal preference. I'd use a list comprehension as I find them quite direct and easy to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev