
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find elements of a list by indices in Python
Consider two lists. The elements in the second list are numbers which needs to be considered as index position for elements of the first list. For this scenario we have the below python programs.
With map and getitem
We can use the getitem magic method is used to access the list items. We can use it along with the map function, so that we get the result from first list which takes the elements from second list as its indics.
Example
listA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1,3] print("Given list A:",listA) print("Given list B:",listB) res=list(map(listA.__getitem__, listB)) print("Result :",res)
Output
Running the above code gives us the following result −
Given list A: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Given list B: [0, 1, 3] Result : ['Mon', 'Tue', 'Thu']
With itemgetter
The operator module provides itemgetter method which can be used for this purpose. In the program below we expand the second list as indices and apply the itemgetter function to get the corresponding elements from the list.
Example
from operator import itemgetter listA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1,3] print("Given list A:",listA) print("Given list B:",listB) res=list((itemgetter(*listB)(listA))) print("Result :",res)
Output
Running the above code gives us the following result −
Given list A: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Given list B: [0, 1, 3] Result : ['Mon', 'Tue', 'Thu']
With numpy
The numpy library can achieve this by just creating an array taking the two lists as input parameters. The result is again converted into a list.
Example
import numpy as np listA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1,3] print("Given list A:",listA) print("Given list B:",listB) res=list(np.array(listA)[listB]) print("Result :",res)
Output
Running the above code gives us the following result −
Given list A: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Given list B: [0, 1, 3] Result : ['Mon', 'Tue', 'Thu']
- Related Articles
- Python Group elements at same indices in a multi-list
- Python program to remove elements at Indices in List
- Python - Ways to find indices of value in list
- Program to get indices of a list after deleting elements in ascending order in Python
- Find indices with None values in given list in Python
- Find the Maximum of Similar Indices in two list of Tuples in Python
- Program to find local peak element indices from a list of numbers in Python
- Program to find minimum possible difference of indices of adjacent elements in Python
- Python – Grouped Consecutive Range Indices of Elements
- Get indices of True values in a binary list in Python
- Python Program to get indices of sign change in a list
- Python – Character indices Mapping in String List
- Find sum of elements in list in Python program
- Find common elements in list of lists in Python
- Find missing elements in List in Python
