Create a list with items from another list at indices specified in a third list

kasperhj

Consider two lists:

a = [2, 4, 5]
b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I want a resulting list c where

c = [0, 0, 2, 0, 4, 5, 0 ,0 ,0 ,0]

is a list of length len(b) with values taken from b defined by indices specified in a and zeros elsewhere.

What is the most elegant way of doing this?

Ffisegydd

Use a list comprehension with the conditional expression and enumerate.

This LC will iterate over the index and the value of the list b and if the index i is found within a then it will set the element to v, otherwise it'll set it to 0.

a = [2, 4, 5]
b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

c = [v if i in a else 0 for i, v in enumerate(b)]

print(c)
# [0, 0, 2, 0, 4, 5, 0, 0, 0, 0]

Note: If a is large then you may be best converting to a set first, before using in. The time complexity for using in with a list is O(n) whilst for a set it is O(1) (in the average case for both).

The list comprehension is roughly equivalent to the following code (for explanation):

c = []
for i, v in enumerate(b):
    if i in a:
        c.append(v)
    else:
        c.append(0)

As you have the option of using numpy I've included a simple method below which uses initialises an array filled with zeros and then uses list indexing to replace the elements.

import numpy as np

a2 = np.array(a)
b2 = np.array(b)

c = np.zeros(len(b2))
c[a2] = b[a2]

When timing the three methods (my list comp, my numpy, and Jon's method) the following results are given for N = 1000, a = list(range(0, N, 10)), and b = list(range(N)).

In [170]: %timeit lc_func(a,b)
100 loops, best of 3: 3.56 ms per loop

In [171]: %timeit numpy_func(a2,b2)
100000 loops, best of 3: 14.8 µs per loop

In [172]: %timeit jon_func(a,b)
10000 loops, best of 3: 22.8 µs per loop

This is to be expected. The numpy function is fastest, but both Jon's function and the numpy are much faster than a list comprehension. If I increased the number of elements to 100,000 then the gap between numpy and Jon's method gets even larger.

Interestingly enough though, for small N Jon's function is the best! I suspect this is to do with the overhead of creating numpy arrays being trumped by the overhead of lists.

Moral of the story: large N? Go with numpy. Small N? Go with Jon.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create list of items from another list

From Dev

Finding indices of items from a list in another list even if they repeat

From Dev

Create list from list items

From Dev

Return values of a list for specific list of indices from another list

From Dev

Excluding items from a list that exist in another list

From Dev

How to create a list of items from another list based on if the items are the key of an object? (Lodash preferred)

From Dev

Remove elements from a list, using another list as indices

From Dev

Changing value of variable in list using indices from another list

From Dev

Summing up elements from one list based on indices in another list

From Dev

Create a list from another list with R

From Dev

java - create a list from subproperties of another list

From Dev

Finding the Indices of multiple items in a list, using a list

From Dev

Subset items in list of list based on indices

From Dev

How to filter one list of items from another list of items?

From Dev

Efficient way to remove element from a list at specified indices

From Dev

Copy list of items to another list

From Dev

Create a list name after another list items in a for loop

From Dev

Finding the indices of the values of one list in another list

From Dev

Remove parts of a list based on another list of indices

From Dev

Remove list of items from another file in bash

From Dev

find lists that start with items from another list

From Dev

Transforming list to dict with list items as keys and list indices as values in python

From Dev

Checking if List contains all items from another list

From Dev

Remove items from a List which are in another List by a specific property?

From Dev

Using LINQ to remove items from a list that do not apear in another list

From Dev

Remove Items from sub list matching another sub list with Linq

From Dev

removing items from list that exist in another list based on an ID member

From Dev

Remove Items from sub list matching another sub list with Linq

From Dev

removing items from list that exist in another list based on an ID member

Related Related

  1. 1

    How to create list of items from another list

  2. 2

    Finding indices of items from a list in another list even if they repeat

  3. 3

    Create list from list items

  4. 4

    Return values of a list for specific list of indices from another list

  5. 5

    Excluding items from a list that exist in another list

  6. 6

    How to create a list of items from another list based on if the items are the key of an object? (Lodash preferred)

  7. 7

    Remove elements from a list, using another list as indices

  8. 8

    Changing value of variable in list using indices from another list

  9. 9

    Summing up elements from one list based on indices in another list

  10. 10

    Create a list from another list with R

  11. 11

    java - create a list from subproperties of another list

  12. 12

    Finding the Indices of multiple items in a list, using a list

  13. 13

    Subset items in list of list based on indices

  14. 14

    How to filter one list of items from another list of items?

  15. 15

    Efficient way to remove element from a list at specified indices

  16. 16

    Copy list of items to another list

  17. 17

    Create a list name after another list items in a for loop

  18. 18

    Finding the indices of the values of one list in another list

  19. 19

    Remove parts of a list based on another list of indices

  20. 20

    Remove list of items from another file in bash

  21. 21

    find lists that start with items from another list

  22. 22

    Transforming list to dict with list items as keys and list indices as values in python

  23. 23

    Checking if List contains all items from another list

  24. 24

    Remove items from a List which are in another List by a specific property?

  25. 25

    Using LINQ to remove items from a list that do not apear in another list

  26. 26

    Remove Items from sub list matching another sub list with Linq

  27. 27

    removing items from list that exist in another list based on an ID member

  28. 28

    Remove Items from sub list matching another sub list with Linq

  29. 29

    removing items from list that exist in another list based on an ID member

HotTag

Archive