Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103997 > unrolled thread
| Started by | vlyamtsev@gmail.com |
|---|---|
| First post | 2016-03-03 14:51 -0800 |
| Last post | 2016-03-04 11:06 +1100 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
list reversal error vlyamtsev@gmail.com - 2016-03-03 14:51 -0800
Re: list reversal error John Gordon <gordon@panix.com> - 2016-03-03 23:08 +0000
Re: list reversal error Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-03 18:13 -0500
Re: list reversal error MRAB <python@mrabarnett.plus.com> - 2016-03-03 23:20 +0000
Re: list reversal error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-03 23:20 +0000
Re: list reversal error Gary Herron <gherron@digipen.edu> - 2016-03-03 15:15 -0800
Re: list reversal error Steven D'Aprano <steve@pearwood.info> - 2016-03-04 11:06 +1100
| From | vlyamtsev@gmail.com |
|---|---|
| Date | 2016-03-03 14:51 -0800 |
| Subject | list reversal error |
| Message-ID | <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> |
i have list of strings "data" and i am trying to build reverse list data1 data1 = [] for i in range(len(data)): j = len(data) - i data1.append(data[j]) but i have the following error: data1.append(data[j]) IndexError: list index out of range am i doing it wrong? Thanks
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2016-03-03 23:08 +0000 |
| Message-ID | <nbag4v$vs$1@reader1.panix.com> |
| In reply to | #103997 |
In <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> vlyamtsev@gmail.com writes:
> i have list of strings "data" and i am trying to build reverse list data1
> data1 = []
> for i in range(len(data)):
> j = len(data) - i
> data1.append(data[j])
> but i have the following error:
> data1.append(data[j])
> IndexError: list index out of range
>
> am i doing it wrong?
> Thanks
Python lists are zero-indexed, meaning a list of five items will have
indexes 0 to 4.
The first time through your loop, i is 0, so
j = len(data) - i
evaluates to
j = len(data)
which would yield 5 for a five-element list, but the last actual element
is in data[4].
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-03-03 18:13 -0500 |
| Message-ID | <mailman.169.1457046848.20602.python-list@python.org> |
| In reply to | #103999 |
On Thu, Mar 3, 2016 at 6:08 PM, John Gordon <gordon@panix.com> wrote: > In <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> > vlyamtsev@gmail.com writes: > > > i have list of strings "data" and i am trying to build reverse list data1 > > data1 = [] > > for i in range(len(data)): > > j = len(data) - i > > data1.append(data[j]) > > > but i have the following error: > > data1.append(data[j]) > > IndexError: list index out of range > > > > am i doing it wrong? > > Thanks > > Python lists are zero-indexed, meaning a list of five items will have > indexes 0 to 4. > > The first time through your loop, i is 0, so > > j = len(data) - i > > evaluates to > > j = len(data) > > which would yield 5 for a five-element list, but the last actual element > is in data[4]. > >>> s = "123" >>> s2 = s[::-1] >>> s2 '321' >>> Use reverse slice > > -- > John Gordon A is for Amy, who fell down the stairs > gordon@panix.com B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com/ <http://joelgoldstick.com/stats/birthdays> http://cc-baseballstats.info/
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-03-03 23:20 +0000 |
| Message-ID | <mailman.170.1457047215.20602.python-list@python.org> |
| In reply to | #103999 |
On 2016-03-03 23:08, John Gordon wrote: > In <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> vlyamtsev@gmail.com writes: > >> i have list of strings "data" and i am trying to build reverse list data1 >> data1 = [] >> for i in range(len(data)): >> j = len(data) - i >> data1.append(data[j]) > >> but i have the following error: >> data1.append(data[j]) >> IndexError: list index out of range >> >> am i doing it wrong? >> Thanks > > Python lists are zero-indexed, meaning a list of five items will have > indexes 0 to 4. > > The first time through your loop, i is 0, so > > j = len(data) - i > > evaluates to > > j = len(data) > > which would yield 5 for a five-element list, but the last actual element > is in data[4]. > A simpler alternative is to use 'reversed': data1 = list(reversed(data))
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-03 23:20 +0000 |
| Message-ID | <mailman.171.1457047299.20602.python-list@python.org> |
| In reply to | #103997 |
On 03/03/2016 22:51, vlyamtsev@gmail.com wrote: > i have list of strings "data" and i am trying to build reverse list data1 > data1 = [] > for i in range(len(data)): > j = len(data) - i > data1.append(data[j]) > > but i have the following error: > data1.append(data[j]) > IndexError: list index out of range At the first pass through the for loop, j is effectively set to len(data). As Python is indexed from zero this will always take you beyond the end of data, hence the IndexError. > > am i doing it wrong? > Thanks > You are doing things the difficult way. First up, using the construct:- for i in range(len(data)): is usually not needed in Python. There is a reversed function https://docs.python.org/3/library/functions.html#reversed which will do the job for you. data1 = list(reversed(data)) Or use the slice notation data1 = data[::-1] -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2016-03-03 15:15 -0800 |
| Message-ID | <mailman.172.1457047506.20602.python-list@python.org> |
| In reply to | #103997 |
On 03/03/2016 02:51 PM, vlyamtsev@gmail.com wrote:
> i have list of strings "data" and i am trying to build reverse list data1
> data1 = []
> for i in range(len(data)):
> j = len(data) - i
> data1.append(data[j])
>
> but i have the following error:
> data1.append(data[j])
> IndexError: list index out of range
>
> am i doing it wrong?
> Thanks
Look at the values (say with a print) you get from your line
j = len(data) - i
You'll find that that produces (with a list of 4 elements for example)
4,3,2,1 when in fact you want 3,2,1,0. Soo you really want
j = len(data) - i -1
Better yet, use more of Python with
data1 = list(reversed(data))
Or don't even make a new list, just reverse the original list in place
>>> L=[1,2,3]
>>> L.reverse()
>>> L
[3, 2, 1]
Or even better, if you simply want to iterate through the original list,
but in reverse order:
for datum in reversed(data):
... whatever with datum ...
which wastes no time actually reversing the list, but simply loops
through them back to front.
Gary Herron
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-03-04 11:06 +1100 |
| Message-ID | <56d8d175$0$1587$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #103997 |
On Fri, 4 Mar 2016 09:51 am, vlyamtsev@gmail.com wrote: > i have list of strings "data" and i am trying to build reverse list data1 Use a slice with a negative step-size and defaults for the start and end positions. data1 = data[::-1] -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web