Python Forum

Full Version: how to easily create a list of already existing item
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
What I am trying to say is without a reason for why you would want to do this it is really difficult to tell you how you could do this. Python is the wild west and there are lots of ways to do most things. Your question seems reasonable to you because you have a context in your head for your question. You have not conveyed that context to anyone else in this thread. Without that context it is very difficult to answer your question.

I did mention local() and dir() which are ways to see attributes defined in a namespace. If you wanted to know how to collect all str type variables into a list I would respond:
x = "hello"
y = 2
z = ["Hello"]

strings = [item for item in locals().values() if isinstance(item, str)]
print(strings)
Output:
['__main__', '...\\testprogram.py', 'hello']
If you wanted to make a list of all variable values where the variable name starts with "my" I might respond this way:
x = "hello"
myInt = 2
myList = ["Hello"]

varslist = [value for key, value in locals().items() if key.startswith("my")]
print(varslist)
Output:
[2, ['Hello']]
But when asked for more detail about your question you prefer to not be helpful, and when asked repeatedly you respond like it is some kind of bullying. In your very first post you said you were "playing around with things". Does that mean there is code? Code is good for providing context.

Possible insight. Are you asking for a way to programmatically collect all the variables that are created by your code and not all the extra stuff that Python automatically creates?
(Dec-29-2021, 10:36 PM)deanhystad Wrote: [ -> ]What I am trying to say is without a reason for why you would want to do this it is really difficult to tell you how you could do this. Python is the wild west and there are lots of ways to do most things. Your question seems reasonable to you because you have a context in your head for your question. You have not conveyed that context to anyone else in this thread. Without that context it is very difficult to answer your question.

I did mention local() and dir() which are ways to see attributes defined in a namespace. If you wanted to know how to collect all str type variables into a list I would respond:
x = "hello"
y = 2
z = ["Hello"]

strings = [item for item in locals().values() if isinstance(item, str)]
print(strings)
Output:
['__main__', '...\\testprogram.py', 'hello']
If you wanted to make a list of all variable values where the variable name starts with "my" I might respond this way:
x = "hello"
myInt = 2
myList = ["Hello"]

varslist = [value for key, value in locals().items() if key.startswith("my")]
print(varslist)
Output:
[2, ['Hello']]
But when asked for more detail about your question you prefer to not be helpful, and when asked repeatedly you respond like it is some kind of bullying. In your very first post you said you were "playing around with things". Does that mean there is code? Code is good for providing context.

Possible insight. Are you asking for a way to programmatically collect all the variables that are created by your code and not all the extra stuff that Python automatically creates?

Saying : "The statement "There are no stupid questions" is a lie. This is a stupid question. It is stupid because it is pointless. " is not how you answer a question...

And if i didn't put any context, it's because i'm looking for a way that can be used in multiple context, so it would be counter productive .
Plus, without a context, it gives you the freedom to solve the problem however you see fit.
Finally, I don't have a context because i litterally only have what I gave you
And i didn't give you a coding exemple, because I don't know how to do this... I know a "for loop" wouldn't work...

Now, thank you for your answer, it's appreciated...

Edit: Yeah now i get it... thanks
(Dec-29-2021, 10:14 PM)CompleteNewb Wrote: [ -> ]
(Dec-29-2021, 09:33 PM)bowlofred Wrote: [ -> ]A list has an order (a sequence). How do you assign the order you want from the objects? What makes the mapping from the desired objects into your list?

I don't have specific criterias. I just wanted to know if there was an easy way to do this...

I think if you could specify criteria, it would be possible. I don't think there's an easy way to do this because usually you would elements containerized rather than transfer them from the locals() or globals() where you have them initially.
(Dec-29-2021, 11:13 PM)bowlofred Wrote: [ -> ]
(Dec-29-2021, 10:14 PM)CompleteNewb Wrote: [ -> ]I don't have specific criterias. I just wanted to know if there was an easy way to do this...

I think if you could specify criteria, it would be possible. I don't think there's an easy way to do this because usually you would elements containerized rather than transfer them from the locals() or globals() where you have them initially.

Well, the two answers Deanhystad gave are great and i'm completly satisfied with both of them. Thank you for your time
Can't stress enough how much of a bad idea something like this is (as has been suggested previously), because you can end up with overcomplicated code that's difficult to understand. In the majority of programs, you really don't need to use things like locals, etc. as there are better ways to design things.

I do speak from experience too - I had to work on a system many years ago where I really had no choice but to use locals and friends. The reason was the other parts of the codebase I needed to use did some very strange things so that was the only way to get access to the data I needed. The code I had to write was unreadable really and unfortunately, it would have been too big a job to rewrite the rest in a more sensible way (and the code was poorly tested, making matters worse).

You'd do well to listen to people's advice when something is a bad idea and spend more time on learning useful techniques.
I do listen to the advice of people and I understand that it is a bad idea. I just wanted to know
Pages: 1 2