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
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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