By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Simple question about Python lists

P: n/a
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of
them how do I select them out. In MATLAB, if I just want the first,
fifth and eighth element I might do something like this:

b = a([1 5 8]);

I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?

Thanks,

Eric
Nov 11 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of them
how do I select them out. In MATLAB, if I just want the first, fifth and
eighth element I might do something like this:

b = a([1 5 8]);

I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
b = [a[i] for i in [1, 5, 8]]

Ciao,
Marc 'BlackJack' Rintsch
Nov 11 '08 #2

P: n/a
On Tue, Nov 11, 2008 at 5:47 PM, Eric <er********@gmail.comwrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of
them how do I select them out. In MATLAB, if I just want the first,
fifth and eighth element I might do something like this:

b = a([1 5 8]);

I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
MATLAB works with 1-based index, while Python is 0-based, so accessing
the index number 8 wouldn't be valid with 8 elements here in Python.

Now, to solve your problem you could either use the already suggested
answer above mine or depending on what you what else you wanna do, you
will feel more comfortable using numpy.

Supposing you have a array with 8 elements: x = numpy.arange(8)
To pull out the first, fifth and eighth elements you would do:
x[numpy.array([0, 4, 7])]

It is so no natural as it was in MATLAB, but well, it was a different language.
Thanks,

Eric
--
http://mail.python.org/mailman/listinfo/python-list


--
-- Guilherme H. Polo Goncalves
Nov 11 '08 #3

P: n/a
On Nov 11, 1:51*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of them
how do I select them out. In MATLAB, if I just want the first, fifth and
eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?

b = [a[i] for i in [1, 5, 8]]

Ciao,
* * * * Marc 'BlackJack' Rintsch
Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.

Regards,

Eric
Nov 11 '08 #4

P: n/a
Guilherme Polo wrote:
On Tue, Nov 11, 2008 at 5:47 PM, Eric <er********@gmail.comwrote:
>I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of
them how do I select them out. In MATLAB, if I just want the first,
fifth and eighth element I might do something like this:

b = a([1 5 8]);

I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?

MATLAB works with 1-based index, while Python is 0-based, so accessing
the index number 8 wouldn't be valid with 8 elements here in Python.

Now, to solve your problem you could either use the already suggested
answer above mine or depending on what you what else you wanna do, you
will feel more comfortable using numpy.

Supposing you have a array with 8 elements: x = numpy.arange(8)
To pull out the first, fifth and eighth elements you would do:
x[numpy.array([0, 4, 7])]
Actually, x[[0,4,7]] will work just as well.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 11 '08 #5

P: n/a
Eric wrote:
On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
>>I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of them
how do I select them out. In MATLAB, if I just want the first, fifth and
eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
b = [a[i] for i in [1, 5, 8]]

Ciao,
Marc 'BlackJack' Rintsch

Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.
In my experience, I never do this with lists so there's no optimized syntax for
it. I do use it very often with numpy arrays, and that does have optimized syntax.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 11 '08 #6

P: n/a
Eric wrote:
... In MATLAB, if I just want the first, fifth and eighth element I
might do something like this: b = a([1 5 8]);
On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netreplied:
>b = [a[i] for i in [1, 5, 8]]
To which Eric said:
Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.
If you are using numpy, you may find found the following link useful:

http://www.scipy.org/NumPy_for_Matlab_Users

--Scott David Daniels
Sc***********@Acm.Org
Nov 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.