
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
\\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: list = ['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: list = [[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]
Printing the First Character of Each List in Python
In Python, we can print the first character of each element in a list in the following ways:
- Using a for loop
- Using list comprehension
- Using map() and a lambda function
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.
At each iteration, we need to append the character at index 0 (of the element) to a new, empty list.
Example: 1
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 -
L1=[[1,1,1],[2,2,2],[3,3,3]] print("First Character of each list: ") first_chars = [] for i in range(len(L1)): first_chars.append(L1[i][0]) print(first_chars)
Output of the above code is:
First Character of each list: [1, 2, 3]
Example: 2
In this example, let us define a list with string values, and obtain the first character of each string using the for loop -
L1 = ['aaa','bbb','ccc'] print("First Character of each string: ") for string in L1: print (string[0])
Following is the output of the above code -
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. The result of the operation is returned as a new list.
To retrieve the first character of each element, we need to use ele[0] as an operation on the desired list.
Example:
In the following example, we are printing the first element of each element of a list of lists using a list comprehension -
L1=[[1,2,3],[4,5,6],[7,8,9]] print("First Character of each list: ") first_chars = [ele[0] for ele in L1] print(first_chars)
On running this code, it will print the following result -
First Character 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 the lambda function to map(), which will find the first element and store it in another list.
Example:
In the following example, we are printing the first element of each sublist (of a list) using the map() function -
L1=[[1,1,1],[2,2,2],[3,3,3]] print("First Character of each list: ") first_chars = list(map(lambda s: s[0], L1)) print(first_chars)
On executing the above code will print the first character from the list as shown below:
First Character of each list: [1, 2, 3]
Conclusion
In this article, we learned how to find the first character of each element in a given Python list. To solve this problem, we traversed the list using a for loop, list comprehension, and the map() function with a lambda expression, and displayed the character at index 0.