Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Slicing List from Kth Element to Last Element
Next article icon

How to get specific elements from list in python

Last Updated : 16 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to get specific item(s) from given list. There are multiple methods to achieve this. Most simple method is accessing list item by Index.

Python
a = [1, 'geeks', 3, 'for', 5]  # accessing specific list item with their indices item1 = a[1] item3 = a[2] item5 = a[4]  # negative indexing to get last element, same as a[4] item_5 = a[-1]  print(item1, item3, item5, item_5) 

Output
geeks 3 5 5 

To get multiple items at once through index, you can create a list of indices and iterate to access those items.

Python
a = [1, 'geeks', 3, 'for', 5]  # list of indices index = [1, 2, 4]  for i in index:   print(a[i])    # comprehensive way of writing above for loop # print([a[i] for i in index]) 

Output
geeks 3 5 

Using List Slicing [ to access range of elements]

By using slicing, you can access range of elements by specifying start and end index.

Python
a = [1, 'geeks', 3, 'for', 5]  # Accessing elements from index 1 to 3 (excluding 4) a1 = a[1:4]  # ['geeks', 3, 'for']  # Accessing elements from start to index 3 (excluding 4) a2 = a[:4]  # [1, 'geeks', 3, 'for']  # Accessing elements from index 2 to the end a3 = a[2:]  # [3, 'for', 5]  # Accessing every second element a4 = a[::2]  # [1, 3, 5]  print(a1, a2, a3, a4) 

Output
['geeks', 3, 'for'] [1, 'geeks', 3, 'for'] [3, 'for', 5] [1, 3, 5] 

Using Slicing is unsuitable if the indices you want are not contiguous or don't follow regular pattern.


Using itemgetter() Function [Pythonic and Efficient Approach]

operator.itemgetter function gives better performance as compared to For Loop or comprehension. It can take multiple indices at once and return a tuple.

Python
from operator import itemgetter  a = [1, 'geeks', 3, 'for', 5]  a1 = itemgetter(1, 3)(a) print(a1) 

Output
('geeks', 'for') 


Get specific elements from a List (based on condition)

To get the elements based on some condition, you can use List comprehension and filter() method. Let's understand them with help of examples.

Using List Comprehension -

Python
a = [10, 20, 30, 40, 50]  # Extract elements greater than 20 a1 = [i for i in a if i > 20]  print(a1)  

Output
[30, 40, 50] 

Using filter() method -

Let's do the same operation as above, using filter() method with lambda function.

Python
a = [10, 20, 30, 40, 50]  # Filter with a lambda function a1 = list(filter(lambda x: x > 20, a))  # [30, 40, 50]  print(a1) 

Output
[30, 40, 50] 

Next Article
Python - Slicing List from Kth Element to Last Element
author
abhishek1
Improve
Article Tags :
  • Python
  • Python Programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • How to Get the Number of Elements in a Python List
    In Python, lists are one of the most commonly used data structures to store an ordered collection of items. In this article, we'll learn how to find the number of elements in a given list with different methods. Example Input: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7 Let's explore various ways t
    3 min read
  • Python | Remove given element from the list
    Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
    7 min read
  • Python - Slicing List from Kth Element to Last Element
    We are given a list we need to slice list from Kth element to last element. For example, n = [10, 20, 30, 40, 50] we nee do slice this list from K=2 element so that resultant output should be [30, 40, 50]. Using List SlicingList slicing allows extracting a portion of a list using the syntax list[k:]
    3 min read
  • Get first element from a List of tuples - Python
    The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
    2 min read
  • Python | Remove given element from list of lists
    The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
    6 min read
  • Remove Multiple Elements from List in Python
    In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
    3 min read
  • Find the List elements starting with specific letter - Python
    The goal is to filter the elements of a list that start with a specific letter, such as 'P'. For example, in the list ['Python', 'Java', 'C++', 'PHP'], we aim to find the elements that begin with the letter 'P', resulting in the filtered list ['Python', 'PHP']. Let's explore different approaches to
    3 min read
  • How to Split Lists in Python?
    Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
    3 min read
  • Python | Remove element from given list containing specific digits
    Given a list, the task is to remove all those elements from list which contains the specific digits.  Examples:  Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] no_delete = ['2', '3', '4', '0'] Output: [1, 5, 6, 7, 8, 9, 11, 15, 16] Explanation: Numbers 2, 3, 4, 10, 12, 13, 14 c
    9 min read
  • Python - Extract Elements from Ranges in List
    We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]]. Using List ComprehensionLis
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences