

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Get first index values in tuple of strings in Python
We have a tuple of strings. We are required to create a list of elements which are the first character of these strings in the tuple.
With index
We design a for loop to take each element and extract the first character by applying the index condition as 0. Then the list function converts it to a list.
Example
tupA = ('Mon', 'Tue', 'Wed', 'Fri') # Given tuple print("Given list : \n" ,tupA) # using index with for loop res = list(sub[0] for sub in tupA) # printing result print("First index charaters:\n",res)
Output
Running the above code gives us the following result −
Given list : ('Mon', 'Tue', 'Wed', 'Fri') First index charaters: ['M', 'T', 'W', 'F']
With next and zip
We apply zip to the tuple and then apply next to get the first character of each element.
Example
tupA = ('Mon', 'Tue', 'Wed', 'Fri') # Given tuple print("Given list : \n" ,tupA) # using next and zip res = list(next(zip(*tupA))) # printing result print("First index charaters:\n",res)
Output
Running the above code gives us the following result −
Given list : ('Mon', 'Tue', 'Wed', 'Fri') First index charaters: ['M', 'T', 'W', 'F']
- Related Questions & Answers
- Python - Get the Index of first element greater than K
- Removing strings from tuple in Python
- How to get First Element of the Tuple in C#?
- Accessing Values of Strings in Python
- Common words among tuple strings in Python
- How to select Python Tuple/Dictionary Values for a given Index?
- Get the index of the first occurrence of a separator in Java
- Sort tuple based on occurrence of first element in Python
- Python Pandas - Indicate duplicate index values except for the first occurrence
- Count the elements till first tuple in Python
- Python Pandas - Return Index with duplicate values removed except the first occurrence
- Get first element of each sublist in Python
- How to index and slice a tuple in Python?
- How can I create a Python tuple of Unicode strings?
- How to get the index and values of series in Pandas?
Advertisements