I wish to know an efficient way and code saving to slice a list of thousand of elements
example:
b = ["a","b","c","d","e","f","g","h"]
index = [1,3,6,7]
I wish a result like as:
c = ["b","d","g","h"]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
The most direct way to do this with lists is to use a list comprehension:
But, depending on exactly what your data looks like and what else you need to do with it, you could use numpy arrays – in which case:
would do what you want, and would avoid the potential memory overhead for large slices – numpy arrays are stored more efficiently than lists, and slicing takes a view into the array rather than making a partial copy.