Python Forum
how to easily create a list of already existing item
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to easily create a list of already existing item
#12
(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
Reply


Messages In This Thread
RE: how to easily create a list of already existing item - by CompleteNewb - Dec-29-2021, 11:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,607 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,598 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
Question Finding string in list item jesse68 8 1,939 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Remove an item from a list contained in another item in python CompleteNewb 19 5,908 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,551 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Create SQLite columns from a list or tuple? snakes 6 8,828 May-04-2021, 12:06 PM
Last Post: snakes
  Time.sleep: stop appending item to the list if time is early quest 0 1,897 Apr-13-2021, 11:44 AM
Last Post: quest
  Create variable and list dynamically quest_ 12 4,515 Jan-26-2021, 07:14 PM
Last Post: quest_
  How to run a pytest test for each item in a list arielma 0 2,393 Jan-06-2021, 10:40 PM
Last Post: arielma
  How to create a linked list and call it? loves 12 4,662 Nov-22-2020, 03:50 PM
Last Post: loves

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020