Python Question about a Python for loop

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Python
AI Thread Summary
The discussion centers around the use of list comprehensions in Python, specifically highlighting a syntax that may be unfamiliar to beginners. A user expresses confusion about the placement of the for loop within the list comprehension syntax, which is a common and efficient way to create lists in Python. It is clarified that this construct is not typically found in older programming languages but is prevalent in Python. The conversation also addresses a user's attempt to replicate this functionality using a traditional for loop, which leads to syntax and conceptual errors in their code. Key points include the importance of testing code snippets in an interactive interpreter to catch errors early and the correct way to use list comprehensions to achieve the desired output. The final example demonstrates the proper use of enumerate with a for loop to achieve the intended result, illustrating the effectiveness of list comprehensions for creating lists from existing data.
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 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 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 member 428835
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
10
Views
3K
Replies
5
Views
1K
Replies
3
Views
1K
Replies
18
Views
1K
Replies
8
Views
2K
Replies
16
Views
2K
Back
Top