# Re: use of index (beginner's question)

Discussion in 'Python' started by Chris Angelico, Apr 28, 2011.

1. ### Chris AngelicoGuest

On Thu, Apr 28, 2011 at 10:42 AM, Rusty Scalf <> wrote:
> list1 = ['pig', 'horse', 'moose']
> list2 =  ['62327', '49123', '79115']
> n = 2
> s2 = "list" + `n`
> a = s2[list1.index('horse')]
> print a

s2 is a string with the value "list2"; this is not the same as the
variable list2. You could use eval to convert it, but you'd do better
to have a list of lists:

lists = [
['pig', 'horse', 'moose']
['62327', '49123', '79115']
]

Then you could use:
n = 2
a = lists[n][list1.index('horse')]

If it helps, you can think of this as a two-dimensional array;
technically it's not, though, it's a list that contains other lists.
(Note that you probably don't want to use the word 'list' as a
variable name; it's the name of the type, and is actually a token in
its own right. But 'lists' or something is fine.)

Chris Angelico

Chris Angelico, Apr 28, 2011

2. ### Thomas 'PointedEars' LahnGuest

Chris Angelico wrote:

> Rusty Scalf wrote:
>> list1 = ['pig', 'horse', 'moose']
>> list2 = ['62327', '49123', '79115']
>> n = 2
>> s2 = "list" + `n`

I would prefer the clearer

s2 = "list" + str(n)

or

s2 = "list%s" % n

>> a = s2[list1.index('horse')]
>> print a

>
> s2 is a string with the value "list2"; this is not the same as the
> variable list2. You could use eval to convert it, but you'd do better
> to have a list of lists:
>
> lists = [
> ['pig', 'horse', 'moose']
> ['62327', '49123', '79115']
> ]

You forgot a comma after the first `]', to separate the list elements.
A way to reuse the existing code is

lists = [list1, list2]

> Then you could use:
> n = 2
> a = lists[n][list1.index('horse')]

That would throw an IndexError exception, though, since list indexes are
0-based, and this list has only two items (so the highest index is 1, not
2).

But even if this was fixed, this could still throw a ValueError exception
if there was no 'horse' in `list1'. While you could catch that â€“

needle = 'horse'
try:
a = lists[n][list1.index(needle)]
except ValueError:
a = 'N/A'

â€“ perhaps a better way to store this data is a dictionary:

data = {
'pig': '62327',
'horse': '49123',
'moose': '79115'
}

print data.get('horse')
print data.get('cow')
print data.get('cow', 'N/A')

Such a dictionary can be built from the existing lists as well:

data = dict(zip(list1, list2))
print data
print data.get('horse', 'N/A')

HTH
--
PointedEars

Thomas 'PointedEars' Lahn, Apr 28, 2011

3. ### Chris RebertGuest

On Wed, Apr 27, 2011 at 6:23 PM, Thomas 'PointedEars' Lahn
<> wrote:
> Chris Angelico wrote:
>> Rusty Scalf wrote:
>>> list1 = ['pig', 'horse', 'moose']
>>> list2 = Â ['62327', '49123', '79115']
>>> n = 2
>>> s2 = "list" + `n`

>
> I would prefer the clearer
>
> Â s2 = "list" + str(n)
>
> or
>
> Â s2 = "list%s" % n

Agreed. The backticks operator is deprecated and was removed in Python 3.

Cheers,
Chris

Chris Rebert, Apr 28, 2011
4. ### Chris AngelicoGuest

On Thu, Apr 28, 2011 at 11:23 AM, Thomas 'PointedEars' Lahn
<> wrote:
> You forgot a comma after the first `]', to separate the list elements.

Whoops! Apologies. It's very confusing when example code has silly
bugs in it! And yes, need to either back down the indices or insert a
shim. Memo, to self: Run the code through IDLE before posting it,
saves people a lot of trouble!

Incidentally, you're allowed to put the comma on the last item too:

lists = [
['pig', 'horse', 'moose'],
['62327', '49123', '79115'],
]

Often makes for easier maintenance, especially when you append
array/list elements.

Chris Angelico

Chris Angelico, Apr 28, 2011
5. ### Algis KabailaGuest

On Thursday 28 April 2011 11:23:51 Thomas 'PointedEars' Lahn
wrote:
> Chris Angelico wrote:
> > Rusty Scalf wrote:
> >> list1 = ['pig', 'horse', 'moose']
> >> list2 = ['62327', '49123', '79115']
> >> n = 2
> >> s2 = "list" + `n`
>>> "list" + 'n'

'listn'
>>>

And IMHO you did not want that, did you?

OldAl.
--
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf

Algis Kabaila, Apr 28, 2011
6. ### Iain KingGuest

On Apr 28, 2:45 am, Chris Angelico <> wrote:
> Incidentally, you're allowed to put the comma on the last item too:
>
>  lists = [
>   ['pig', 'horse', 'moose'],
>   ['62327', '49123', '79115'],
> ]
>
> Often makes for easier maintenance, especially when you append
> array/list elements.
>
> Chris Angelico

I did not know this. Very useful!

Iain

Iain King, Apr 28, 2011