0

Right I am a complete beginner here, trying to work through various python tutorials and a lot of them are great. However none of them have much of a consolidated approach to learning, where one skill builds on another. They all seem to show you things in silo. All I am trying to do is concatenate a string with an integer that is held in an array. Here is the code:

text = 'product_price_'
numberArray = [1,2,3,4,5,6,7,8,9,10]


for i in numberArray:
  print text + str(numberArray[i])

This kind of works, giving me the result:

product_price_2
product_price_3
product_price_4
product_price_5
product_price_6
product_price_7
product_price_8
product_price_9
product_price_10
Traceback (most recent call last):
  File "/Users/me/Documents/Programming/python/eclipse/workspace/concat.py", line 8, in <module>
print text + str(numberArray[i])
IndexError: list index out of range

Like I say this is really simple stuff. I can concatenate, I can print an array but do both??

Can anyone plug this gap in my knowledge for me?

Thanks

3
  • 2
    You did concetate. Perfectly. 10 times. "list index out of range" has nothing to do with concatenation. It has everything to do with your list of numbers. And the index i. You're not showing all of the code in your script. Please be sure to include line #8 (the one named in the error message). Commented Jan 13, 2012 at 12:14
  • line 1 was an import re declaration which was left over from another exercise. I appreciate that the error is not to do with concatenation, but the point of the whole exercise is. Commented Jan 13, 2012 at 12:21
  • "I appreciate that the error is not to do with concatenation". Good. However. Since the error has nothing to do with concatenation, you need to change the title and ask "Why is this index out of range". That's the error you're getting because your for loop reflects a failure to understand list indexes. Commented Jan 13, 2012 at 12:26

6 Answers 6

6

i contains the value from the array, not the index. So if you want to concatenate the value, try this:

for i in numberArray:
  print text + str(i)
Sign up to request clarification or add additional context in comments.

5 Comments

sorry to duplicate your answer. it wasn't there when I wrote my one
Thanks all! So I needed to print the index not the value. Got it. Never ceases to amaze me the speed of responses on this board. Such a valuable resource for beginners.
@Peter: In fact I think you wanted to print the values. The correction is you don't need to worry about index when you are using 'in' with a for loop
"I needed to print the index not the value.". False. for i in numberArrray does not give indexes. It gives the actual values from numberArray. Not indexes.
"So I needed to print the index not the value." No; you need to print the value. i is the value, no matter how you've named that variable.
2

Long story short:

text = 'product_price'
numberArray = [1,2,3,4,5,6,7,8,9,10]

for num in numberArray:
    print '_'.join((text, str(num)))

Long story long:

- Step 1

You should not confuse the index with the value. You're example "kind of worked" because you stored numbers in you array (which by the way is a list), but since the index numeration start with 0 you skipped the first element and found your list index out of range after the last one.

This is a fix for your example

text = 'product_price_'
numberArray = [1,2,3,4,5,6,7,8,9,10]

for i in numberArray:
    print text + str(i)

- Step 2

Try storing string in your list and maybe something will be more clear:

>>> text = 'product_price '
>>> my_list = ['one', 'two', 'three']
>>> for price in my_list:
...     print text + price
product_price one
product_price two
product_price three

In python it's really not necessary to get the value from its index, so you should not be doing:

>>> text = 'product_price '
>>> my_list = ['one', 'two', 'three']
>>> for i in range(len(my_list)):
...     print text + my_list[i]
product_price one
product_price two
product_price three

- Step 3

The last step is to concatenate strings using str.join(), in most of the case this will be more efficent:

>>> text = 'product_price'   # without the underscore at the end
>>> numberArray = [1,2,3,4,5,6,7,8,9,10]
>>> for num in numberArray:
...     print '_'.join((text, str(num)))
product_price_1
product_price_2
product_price_3
[...]

Comments

1

You may want this code

for i in range(len(numberArray)):
    print text + str( numberArray[i] )

Comments

0

You could use a list comprehension:

[text + str(i) for i in numberArray]

Or even shorter:

[text + str(i) for i in range(11)]

to get rid of the number list (it's a list, not an array).

Another way is to use map:

map(lambda i: b + str(i), a)

Comments

0

You want to loop on the indeces of numberArray:

for i in xrange(len(numberArray)):
    print text + str(numberArray[i])

then i takes values 0 to 9, and your list-index will not be out of range.

The function xrange gives you an itearator on the range 0,...,len(numberArray) - 1. Since the length of numberArray is 10 you have an iterator from 0 to 9.

Comments

0

Maybe what you want is

for i in numberArray:

    print text + str(i)

When using 'in' operator, array elements are cycled through and not indexes. And array indexes start with 0 and not 1. eg

numberArray[0] = 1

numberArray[1] = 2

numberArray[9] = 10

If you insist on using indexes for this example, Your array should change to

numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3 Comments

"Your code will work if your array changes ..." doesn't seem as helpful as the rest of the answer. While it's true, it makes the "index-value" confusion even worse than it was.
That's better, but doesn't clarify the index-value confusion at all. It sort of assumes that everyone knows that the incorrect program in the question confused indexes and values.
yes :), as other answers already made him realize, I don't think I can explain it any better. Updated the answer because it was slightly misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.