Last updated: Apr 10, 2024
Reading timeยท6 min
To get the first character of each string in a list:
0
and return the result.my_list = ['bobby', 'hadz', 'com'] new_list = [item[0] for item in my_list] print(new_list) # ๐๏ธ ['b', 'h', 'c']
We used a list comprehension to iterate over the list.
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.
This is a three-step process:
for
loop to iterate over the original list.my_list = ['bobby', 'hadz', 'com'] new_list = [] for item in my_list: new_list.append(item[0]) print(new_list) # ๐๏ธ ['b', 'h', 'c']
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.
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.
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.
my_list = ['bobby', 'hadz', 'com'] first = my_list[0][0] print(first) # ๐๏ธ 'b' print(my_list[0][1]) # ๐๏ธ 'o'
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.
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.
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.
my_list = ['', 'hadz', 'com'] try: print(my_list[0][0]) except IndexError: # ๐๏ธ this runs print('The specified index is out of range.')
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.
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]
.
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.
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.
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.
To get the first letter of each word in a string:
str.split()
method to split the string into a list of words.0
and return the result.my_str = 'my site is bobbyhadz.com' my_list = [word[0] for word in my_str.split()] print(my_list) # ๐๏ธ ['m', 's', 'i', 'b']
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.
my_str = 'my site is bobbyhadz.com' # ๐๏ธ ['my', 'site', 'is', 'bobbyhadz.com'] print(my_str.split())
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.
my_str = 'my site is bobbyhadz.com' my_list = [word[0] for word in my_str.split()] print(my_list) # ๐๏ธ ['m', 's', 'i', 'b']
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.
This is a three-step process:
str.split()
method to split the string into a list of words.for
loop to iterate over the list.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']
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.
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.
You can learn more about the related topics by checking out the following tutorials: