Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
nHow to print out the first character of each list in Python?
A Python list is a built-in, mutable datatype that stores multiple items or elements, separated by commas, within square brackets [ ]. The index of a list in Python starts from 0 up to length-1. We can retrieve/access elements at a particular index as follows ?
list_name[index]
The given task is to write a Python program that prints the first character of each element in a list. But, before that, let's see some example scenarios:
Scenario 1
For example, if our list contains string values, the output should be the first character of each string.
Input: names = ['aaa','bbb','ccc'] Output: new_list = [a, b, c] Explanation: Look at the input list, it has 3 strings. The first string starts with character 'a' The second one with 'b' The third one with 'c'. Hence, the output is [a, b, c].
Scenario 2
Similarly, if our list contains (sub) lists, the output should be the first element of each sublist.
Input: numbers = [[12, 123, 1234], [9, 98, 987], [56, 456, 3475]] Output: new_list = [12, 9, 56] Explanation: The input list has 3 sublists. The first element of 1st sublist is '12' The first element of 2nd sublist is '9' The first element of the 3rd sublist is '56'. Therefore, the result would be [12, 9, 56]
Using a for Loop
In Python, using the for loop, we can iterate through each element in any iterable object. To print or retrieve the first characters of each element in a list, we need to iterate through its elements and access the character at index 0.
Example 1: First Element from Sublists
In the following Python program, we are defining a list of lists, and then using a for loop, we are printing the first element of each sublist ?
numbers = [[1,1,1],[2,2,2],[3,3,3]]
print("First element of each list:")
first_elements = []
for sublist in numbers:
first_elements.append(sublist[0])
print(first_elements)
First element of each list: [1, 2, 3]
Example 2: First Character from Strings
In this example, let us define a list with string values, and obtain the first character of each string using the for loop ?
names = ['apple','banana','cherry']
print("First character of each string:")
first_chars = []
for string in names:
first_chars.append(string[0])
print(first_chars)
First character of each string: ['a', 'b', 'c']
Using List Comprehension
The list comprehension in Python helps us to create a list by performing an operation on each element of an iterable. To retrieve the first character of each element, we use element[0] as an operation on the desired list.
Example
In the following example, we are extracting the first element of each sublist using list comprehension ?
numbers = [[1,2,3],[4,5,6],[7,8,9]]
print("First element of each list:")
first_elements = [sublist[0] for sublist in numbers]
print(first_elements)
First element of each list: [1, 4, 7]
Using map() Function
The Python map() function accepts a function and an iterable as parameters, applies the specified function to each item in the given iterable. To find the first character from each element in a list, we pass a lambda function to map().
Example
In the following example, we are extracting the first element of each sublist using the map() function ?
numbers = [[1,1,1],[2,2,2],[3,3,3]]
print("First element of each list:")
first_elements = list(map(lambda s: s[0], numbers))
print(first_elements)
First element of each list: [1, 2, 3]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop | High | Good | Beginners, complex operations |
| List Comprehension | High | Best | Pythonic, concise code |
| map() with lambda | Medium | Good | Functional programming style |
Conclusion
Python provides multiple ways to extract the first character or element from each item in a list. List comprehension offers the most Pythonic and efficient approach, while for loops provide better readability for complex operations.
