-1

I have a situation where I need to populate a list that is going to be a part of a larger data structure with a series of identical values. Now, in any other programming language I would do it for example like this (let itemIndexes be a list of item indexes):

targetStatusCode = "TC13"
statusCodes = []
foreach itemIndexes:
    statusCodes .append(targetStatusCode)

However, in Python, this is apparently not possible.

Instead, I have now settled for this:

targetStatusCode = "TC13"
statusCodes = []
for each in itemIndexes:
    statusCodes .append(targetStatusCode)

However, this results in me getting a warning "local variable 'each' is not used anymore".

So, here's my question: Is there an intended "ideal" way to handle this specific sort of situation in python that does not generate a warning?


Explanation why this is not a duplicate of for loop in Python

I already tried the above solution, and it raises the IDE warning I described above. My question was for a "clean" solution that allows me to do this without my IDE raising a "variable is not used anymore"-warning. One of the answers below thankfully provided a working solution for this.

Kira Resari
  • 1,718
  • 4
  • 19
  • 50
  • 3
    `statusCodes = [targetStatusCode] * len(itemIndexes)` ? no loops at all – DeepSpace Jun 26 '19 at 13:42
  • Your question and it's description... It seems that you did not tried reading python documentation at all. Take a look at [this great question](https://stackoverflow.com/questions/9884132/what-exactly-are-iterator-iterable-and-iteration) for instance – Lucas Wieloch Jun 26 '19 at 13:44
  • 1
    And anyway, whatever you used that threw the warning, it's just an information that something wrong **might** be going on and that you might have to give it a second look, nothing more. As a side note, it's a common convention in Python to use `_` as variable name in such situations. – Thierry Lathuille Jun 26 '19 at 13:48
  • 1
    The supposed duplicate asks how to loop over integers with a step different from 1, which is unrelated to the question asked here - which luckily got useful answers before being closed. – Thierry Lathuille Jun 26 '19 at 14:06

2 Answers2

2

Since you're adding strings specifically (more complex objects would run into issues with duplicated references), you could probably just use a list literal for this:

statusCodes = [targetStatusCode] * len(itemIndexes)

If you feel the need to use a for loop in particular, then you're doing it correctly - the IDE you're using probably shouldn't be yelling at you. But to make it more clear that you're using the for loop to iterate just a specific number of times, you can change what you call the loop control variable:

for _ in itemIndexes:
    statusCodes.append(targetStatusCode)

in which case, if your IDE or linter gives you a warning, you should look into its settings and see if you can disable that warning.


It also seems like you have a misconception about for loops in python. You imply that foreach loops don't exist in python, when, in fact, all for loops are foreach loops in python. The syntax is for var in iterable:, where iterable must be some object that can be iterated through (to iterate through the values 0 through 9, you use the range(10) function instead of the C-style for (i=0; i < 10; i++))

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • Thank you very much, Green Cloak Guy! The `for _ in itemIndexes` works great and also doesn't raise a warning in my IDE. I don't want to disable these warnings because they are warnings that inform me of unused variables. – Kira Resari Jun 28 '19 at 06:42
2

The most straightforward solution, since all your 'terms' are the same, is to simply do:

statusCodes = ["TC13"] * len(itemIndexes)

However, if you wanted to iterate over the items and possibly use their values while constructing statusCodes, the most pythonic way is to use List Comprehension:

statusCodes = [ "TC13" for item in itemIndexes ]