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 8140827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:17:37+00:00 2026-06-06T12:17:37+00:00

For example, I have two lists of the same length: a = [[1,2],[2,2],[3,3],[4,2],[5,6]] b

  • 0

For example, I have two lists of the same length:

a = [[1,2],[2,2],[3,3],[4,2],[5,6]]
b = [1,2,2,3,1]

I want to have a function such that

func(a,b,1) = [[1,2],[5,6]]
func(a,b,2) = [[2,2],[3,3]]
func(b,b,2) = [2,2]

What the function does is return a list of a’s elements, whose corresponding elements of the same index in list b equal to the third argument.

In Matlab I will do something as easy as a(b==1), a(b==2), b(b==2). What is the most efficient way to achieve this in Python?

  • 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-06-06T12:17:39+00:00Added an answer on June 6, 2026 at 12:17 pm

    If you want very Matlab-like functionality, you could use numpy:

    >>> import numpy
    >>> a = [[1,2],[2,2],[3,3],[4,2],[5,6]]
    >>> b = [1,2,2,3,1]
    >>> a = numpy.array(a)
    >>> b = numpy.array(b)
    >>> a[b==1]
    array([[1, 2],
           [5, 6]])
    >>> a[b==2]
    array([[2, 2],
           [3, 3]])
    >>> b[b==2]
    array([2, 2])
    

    Failing that, I’d probably simply use a list comprehension:

    >>> [i for i,j in zip(a,b) if j == 1]
    [[1, 2], [5, 6]]
    >>> [i for i,j in zip(a,b) if j == 2]
    [[2, 2], [3, 3]]
    >>> [i for i,j in zip(b,b) if j == 2]
    [2, 2]
    

    It’d be trivial to wrap this in a function:

    >>> def func(a,b,x):
    ...     return [i for i,j in zip(a,b) if j == x]
    ... 
    >>> func(a,b,2)
    [[2, 2], [3, 3]]
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python, compute list difference I have two lists For example: A =
I want to compare two List of different lengths and/or same length. The first
I want to calculate , in two lists (same lenght), the number of elements
Say I have two lists or arrays of strings. For example: list 1: a,
I have two custom dropdown lists that have the same markup. I need to
I have two lists [a,b,c,d] and [b,d,a,c] How can I make a function that
I have two lists of double values which have the same length. Is it
I have two lists based on the same class. I would like one list
I have two ordered lists of the same element type, each list having at
We have two A records pointing to same public IP address as: www.example.com IN

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.