•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,466 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 2103 | Replies: 4
![]() |
•
•
Join Date: Jun 2007
Posts: 27
Reputation:
Rep Power: 2
Solved Threads: 1
test = [random.randint(1,10) for i in range(20)]
equals
>>> import random
>>> test=[]
>>> for i in range(20):
#test.append(random.randint(1, 10))
random_int=random.randint(1, 10)
test.append(random_int) Last edited by StrikerX11 : Sep 13th, 2007 at 12:28 am.
•
•
Join Date: Aug 2007
Location: Johannesburg, South Africa
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
The square brackets are part of a construction called a 'list comprehension'. The basic syntax is
[x for blah in yourlist]
The idea is to create a list quickly in one line. And as StrikerX11 noted, it's exactly the same as
(Usually, x and blah have some relationship to each other, but as you can see in this case, not always).
I wonder what the clearest way to write that would be. I found myself saying, "That's how I'd do it..."
Jeff
[x for blah in yourlist]
The idea is to create a list quickly in one line. And as StrikerX11 noted, it's exactly the same as
Python Syntax (Toggle Plain Text)
tmp = [] for blah in yourlist: tmp.append(x)
(Usually, x and blah have some relationship to each other, but as you can see in this case, not always).
•
•
•
•
Originally Posted by grobister
It's not really written the "python way" which encourages transparent readable code!
I wonder what the clearest way to write that would be. I found myself saying, "That's how I'd do it..."
Jeff
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Starting Python (Python)
- Loop question (Python)
- Simple script loop question (Shell Scripting)
- Asking for help in very simple Python (Python)
- very 1st python tutorial 4 newbies (Python)
- while loop question (Python)
- need help w/for loop (C++)
Other Threads in the Python Forum
- Previous Thread: newbee: Simple Backend Python Script Question
- Next Thread: Generating HTML in Python



Linear Mode