Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:33:56+00:00 2026-05-23T14:33:56+00:00

I have the following Python list (can also be a tuple): myList = [‘foo’,

  • 0

I have the following Python list (can also be a tuple):

myList = ['foo', 'bar', 'baz', 'quux']

I can say

>>> myList[0:3]
['foo', 'bar', 'baz']
>>> myList[::2]
['foo', 'baz']
>>> myList[1::2]
['bar', 'quux']

How do I explicitly pick out items whose indices have no specific patterns? For example, I want to select [0,2,3]. Or from a very big list of 1000 items, I want to select [87, 342, 217, 998, 500]. Is there some Python syntax that does that? Something that looks like:

>>> myBigList[87, 342, 217, 998, 500]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:33:56+00:00Added an answer on May 23, 2026 at 2:33 pm
    list( myBigList[i] for i in [87, 342, 217, 998, 500] )
    

    I compared the answers with python 2.5.2:

    • 19.7 usec: [ myBigList[i] for i in [87, 342, 217, 998, 500] ]

    • 20.6 usec: map(myBigList.__getitem__, (87, 342, 217, 998, 500))

    • 22.7 usec: itemgetter(87, 342, 217, 998, 500)(myBigList)

    • 24.6 usec: list( myBigList[i] for i in [87, 342, 217, 998, 500] )

    Note that in Python 3, the 1st was changed to be the same as the 4th.


    Another option would be to start out with a numpy.array which allows indexing via a list or a numpy.array:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    

    The tuple doesn’t work the same way as those are slices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
Possible Duplicate: Flatten (an irregular) list of lists in Python I have the following
How can I pad a list when printed in python? For example, I have
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I have the following simple python test script that uses Suds to call a
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following simple methods for writing a python object to a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.