
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Removing strings from tuple in Python
- Python - Get the Index of first element greater than K
- Common words among tuple strings in Python
- How to select Python Tuple/Dictionary Values for a given Index?
- Accessing Values of Strings in Python
- How to get First Element of the Tuple in C#?
- Python program to get first and last elements from a tuple
- Python Pandas - Indicate duplicate index values except for the first occurrence
- Sort tuple based on occurrence of first element in Python
- How to index and slice a tuple in Python?
- Count the elements till first tuple in Python
- Get the index of the first occurrence of a separator in Java
- Get tuple element data types in Python
- Get minimum difference in Tuple pair in Python
- Python Pandas - Return Index with duplicate values removed except the first occurrence

Advertisements