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

Discussion Overview

The discussion revolves around the use of for loops in Python, specifically focusing on the concept of list comprehensions. Participants explore the syntax and functionality of list comprehensions compared to traditional for loops, as well as common pitfalls in their usage.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the positioning of for loops in list comprehensions, noting they have only seen traditional for loops at the beginning of lines.
  • Another participant identifies the construct as a "list comprehension," stating it is a common feature in Python.
  • A third participant adds that list comprehensions may not be present in older programming languages, suggesting they are more common in newer languages.
  • A participant points out syntax and conceptual errors in the original code provided, emphasizing the importance of testing code snippets in an interactive interpreter.
  • Multiple examples are provided to illustrate the errors in the original approach and to demonstrate the correct usage of list comprehensions and for loops.

Areas of Agreement / Disagreement

Participants generally agree on the definition and utility of list comprehensions in Python, but there is disagreement regarding the effectiveness of the original code approach and the understanding of its syntax.

Contextual Notes

Limitations include potential misunderstandings of list comprehensions versus traditional for loops, as well as unresolved issues regarding the syntax and assignment in the original code example.

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
4K
  • · 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