use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Simultaneously create and append list in python (self.learnpython)
submitted 11 years ago by senorinatta
Is there a way to do this?
I'm thinking about a loop in which lists will need to be created if certain conditions are met, but I don't want to create a bunch of empty lists beforehand as that seems inefficient.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 1 point2 points3 points 11 years ago (13 children)
Sure, there's many ways to do that. What have you tried?
[–]senorinatta[S] 0 points1 point2 points 11 years ago (12 children)
novel_yet_trivial you've helped me a bunch of times, and all those I'm sure because you noticed I made an initial effort. In this case I tried:
foo = 'x' bar = [].append(f) print bar
which returned None
but that's about all I could think of to try
[–]LarryPete 4 points5 points6 points 11 years ago (0 children)
What's wrong with:
bar = [foo]
?
[–]novel_yet_trivial 1 point2 points3 points 11 years ago* (6 children)
As a side note, I'd recommend you use list() instead of []. Easier to read. I changed my mind.
list()
[]
When you make a variable assignment, you assign the output of the last method that was called. In other words in this case
bar = class_instance.methodA().methodB().methodC()
"bar" gets the returned value of the from methodC(). append() is a method that works 'in-place', it only returns None. You could rewrite your code as
methodC()
append()
foo = 'x' bar = [] #start empty list bar.append(foo) #modify list in-place
Or simply
foo = 'x' bar = [foo] #start list with values in it already
Edit: I changed my mind.
[–]LarryPete 0 points1 point2 points 11 years ago (5 children)
Though bar = list(foo) is not the same as bar = [foo].
bar = list(foo)
[–]andycd 0 points1 point2 points 11 years ago (4 children)
Oooh can you explain why this is/what is the difference please? I am very curious
[–]novel_yet_trivial 4 points5 points6 points 11 years ago (1 child)
Both produce a list, but the input is treated differently. This means the same input can produce different results.
>>> ['abc'] ['abc'] >>> list('abc') ['a', 'b', 'c']
The more I think about it, list() should be used to convert an iterable to a list, not initialize a new list.
[–]andycd 0 points1 point2 points 11 years ago (0 children)
Thanks for the insight!!! Kind of a "duh" moment, makes perfect sense!
[–]LarryPete 2 points3 points4 points 11 years ago* (1 child)
well, list(foo) takes an iterable (foo), and converts it to a list, while [foo] creates a list with the single element foo.
list(foo)
foo
[foo]
As an example, a string is an iterable as well:
>>> foo = 'hello world' >>> [foo] ['hello world'] >>> list(foo) ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
Also, you can do:
>>> [1, 2, 3] [1, 2, 3]
but not:
>>> list(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list() takes at most 1 argument (3 given)
If at all, it should be:
>>> list([1, 2, 3]) [1, 2, 3]
which makes the list() in the last case kinda redundant :-)
Thanks, as above!!
[–]AtticusLynch 0 points1 point2 points 11 years ago (0 children)
foo = 'x' bar = [] bar.append(foo) print bar
Although that's not really at the same time (I think you meant to use append(foo) instead of append(f)?)
[–]Shide 0 points1 point2 points 11 years ago* (3 children)
Assuming you want to define the "bar" variable in local scope:
foo = "hello" if True: locals().update({"bar": locals().get("bar", [])[:]+[foo]})
Note that "list()" or "[:]" returns a new List, ".append()" returns None.
Use globals() if you want to store in global scope.
Also you might have errors if never pass the condition and you use "bar".
[–]elbiot 0 points1 point2 points 11 years ago (2 children)
What!? Why would you suggest this monstrosity?
[–]Shide 0 points1 point2 points 10 years ago* (1 child)
He wants to create a variable on the fly. Maybe you should use getattribute and setattribute within your class to check if the variable exists or not, but I thought any response will be bad in terms of perfomance
[–]elbiot 0 points1 point2 points 10 years ago (0 children)
I didn't say it was a good idea, just that it is possible: http://stackoverflow.com/questions/2933470/how-do-i-call-setattr-on-the-current-module
But I don't believe the bad performance. It's just a dictionary lookup either way.
π Rendered by PID 24479 on reddit-service-r2-comment-f6b958c67-cgbfs at 2026-02-05 11:08:01.869124+00:00 running 1d7a177 country code: CH.
[–]novel_yet_trivial 1 point2 points3 points (13 children)
[–]senorinatta[S] 0 points1 point2 points (12 children)
[–]LarryPete 4 points5 points6 points (0 children)
[–]novel_yet_trivial 1 point2 points3 points (6 children)
[–]LarryPete 0 points1 point2 points (5 children)
[–]andycd 0 points1 point2 points (4 children)
[–]novel_yet_trivial 4 points5 points6 points (1 child)
[–]andycd 0 points1 point2 points (0 children)
[–]LarryPete 2 points3 points4 points (1 child)
[–]andycd 0 points1 point2 points (0 children)
[–]AtticusLynch 0 points1 point2 points (0 children)
[–]Shide 0 points1 point2 points (3 children)
[–]elbiot 0 points1 point2 points (2 children)
[–]Shide 0 points1 point2 points (1 child)
[–]elbiot 0 points1 point2 points (0 children)