Question about a Python for loop

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Python
Click For Summary
SUMMARY

The discussion centers on the use of list comprehensions in Python, specifically the syntax and functionality of the construct. A user inquires about the unusual positioning of a for loop within a list comprehension, which is confirmed as a common and efficient practice in Python. The conversation highlights the importance of understanding syntax errors and conceptual errors when using for loops, particularly in Python 3. The final correct implementation demonstrates how to convert a list of integers to a list of strings using list comprehension effectively.

PREREQUISITES
  • Understanding of Python 3 syntax
  • Familiarity with list comprehensions
  • Basic knowledge of error handling in Python
  • Experience with the interactive Python interpreter
NEXT STEPS
  • Study the Python documentation on list comprehensions
  • Practice using the interactive Python interpreter for testing code snippets
  • Learn about the differences between Python 2 and Python 3 regarding syntax
  • Explore advanced list manipulation techniques in Python
USEFUL FOR

Beginner to intermediate Python developers, educators teaching Python programming, and anyone looking to improve their understanding of Python's list comprehensions and error handling.

member 428835
Hi All

I'm learning python and have never seen a for loop positioned like this:
Python:
integers = [4, 5, 7]
strings = [str(integer) for integer in integers]
I've only ever seen for loops positioned at the beginning of lines. Is this usual? How can I know when this is okay? I think I would have tried something like this instead
Python:
integers = [4, 5, 7]
strings = range(len(integers))
for i in integers:
    strings(i) = [str(i)]
 
Technology news on Phys.org
It's called a "list comprehension" and is a fairly common construct, yes.
 
  • Like
Likes   Reactions: member 428835
Ibix said:
It's called a "list comprehension" and is a fairly common construct, yes.
In Python... It's not a construct that's present in other, older languages, AFAIK. Possibly it's present in some of the newer languages.
 
  • Like
Likes   Reactions: Ibix, member 428835 and phinds
joshmccraney said:
I think I would have tried something like this instead
Your code has a syntax error in its last line. It also has two conceptual errors (three if you are using Python 3).

A really good piece of advice is to test any snippet of code in the interactive interpreter before posting it. Even better, post the actual interpreter session:

Python:
>>> integers = [4, 5, 7]
>>> strings = range(len(integers))
>>> for i in integers:
...     strings(i) = [str(i)]
...
  File "<stdin>", line 2
SyntaxError: cannot assign to function call
>>> for i in integers:
...     strings[i] = [str(i)]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'range' object does not support item assignment
>>> strings = list(range(len(integers)))
>>> for i in integers:
...     strings[i] = [str(i)]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list assignment index out of range
>>> for i in integers:
...     print(i)
...     strings[i] = [str(i)]
...
4
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: list assignment index out of range
>>> for idx, i in enumerate(integers):
...     strings[idx] = [str(i)]
...
>>> strings
[['4'], ['5'], ['7']]
>>> for idx, i in enumerate(integers):
...     strings[idx] = str(i)
...
>>> strings
['4', '5', '7']
Only the very last is actually what is intended.
 
  • Like
Likes   Reactions: member 428835

Similar threads

  • · Replies 11 ·
Replies
11
Views
1K
Replies
55
Views
7K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K