Selecting Multiple Parts of a List

Select multiple elements from a list

mylist[c(5,7,9)] should do it.

You want the sublists returned as sublists of the result list; you don't use [[]] (or rather, the function is [[) for that -- as Dason mentions in comments, [[ grabs the element.

Explicitly select items from a list or tuple

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.

Correct way to select multiple list elements by name

The easiest way is to use the [ operator like:

mylist[c("listA", "listC")]

Output:

$`listA`
[1] 1 2 3 4 5 6 7 8 9 10

$listC
[1] 25 26 27 28 29 30 31 32

Note that when selecting one element from a list using [, the output might not be what we expect:

mylist["listA"]

Output:

$`listA`
[1] 1 2 3 4 5 6 7 8 9 10

> class(mylist["listA"])
[1] "list"

Here we see that selecting the element "listA" using [ does not return the element itself, but instead, returns a list that contains the "listA" element. If we want to subset the element itself by name, we should use the [[ operator:

mylist[["listA"]]

Output:

[1]  1  2  3  4  5  6  7  8  9 10

> class(mylist[["listA"]])
[1] "integer"

Another difference between [ and [[ is that [[ can only be used to select a single element. For example, the following would not work:

mylist[[c("listA", "listC")]]

Error in mylist[[c("listA", "listC")]] : subscript out of bounds

How to select multiple elements from multiple vectors in a list

We could use lapply

lapply(mylist[1:2], `[`, c(1, 3))

#[[1]]
#[1] -0.5604756 1.5587083

#[[2]]
#[1] 6 55

which is similar to map in purrr

purrr::map(mylist[1:2], `[`, c(1, 3))

To update the values of selected elements, we can do

mylist[1:2] <-lapply(mylist[1:2], function(x) {x[c(1, 3)] <- 0;x})
mylist
#[[1]]
#[1] 0.00000000 -0.23017749 0.00000000 0.07050839 0.12928774

#[[2]]
#[1] 0 61 0 8 8

#[[3]]
#[1] 4 3 8 7 6

data

set.seed(123)
mylist<- list(rnorm(5), rgeom(5, 0.05), rbinom(5, 10, 0.5))

Access multiple elements of list knowing their index

You can use operator.itemgetter:

from operator import itemgetter 
a = [-2, 1, 5, 3, 8, 5, 6]
b = [1, 2, 5]
print(itemgetter(*b)(a))
# Result:
(1, 5, 5)

Or you can use numpy:

import numpy as np
a = np.array([-2, 1, 5, 3, 8, 5, 6])
b = [1, 2, 5]
print(list(a[b]))
# Result:
[1, 5, 5]

But really, your current solution is fine. It's probably the neatest out of all of them.

Is it possible to extract multiple items from a list in R with map()?

Something like this?

library(purrr)
listy %>% map(., function(x) c(x[[1]], x[[2]]))

$A
[1] "silence" "cats"

$B
[1] "silence" "fish-head"

To get the output in the form of a data.frame,

listy %>% map_df(., function(x) c(x[[1]], x[[2]]))

# A tibble: 2 x 2
A B
<chr> <chr>
1 silence silence
2 cats fish-head

Or, as suggested by @Richard Scriven,

map(listy, ~ unlist(.[1:2]))

Javascript to select/highlight multiple parts of a text

Found one that does exactly what I want (+it's compatible with really old browsers)
https://github.com/mir3z/texthighlighter

Select multiple sections of rows by index in pandas

One possible solution with concat:

cdf = pd.concat([df.loc[11:13], df.loc[17:20]])
print (cdf)
A B
11 1 b
12 2 c
13 3 d
17 7 h
18 8 i
19 9 j

Another solution with range:

cdf = df.loc[list(range(11,14)) + list(range(17,20))]
print (cdf)
A B
11 1 b
12 2 c
13 3 d
17 7 h
18 8 i
19 9 j


Related Topics



Leave a reply



Submit