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
#1
hello, simple question here. If i have a series of items how can i not manually create a list?
Like

a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=8
i=9
j=10

#do something

results:

list1 = [ 1,2,3,4,5,6,7,8,9,10]

I know you can do it manually, like i just did, but is there a way to automate it?
N.B: i gave an exemple for variables, but it can be any items, even objects.
Reply
#2
This is not something you should ever need to do. That you think you need something like this is a flashing neon sign that says "This is a BAD design!"

Why do you have a bunch of variables that you want to collect into a list? If you have things that should be collections, they should be created as collections. Variables for individual objects should be fairly rare in programs, usually existing for fleeting moments in time where they are used to represent an item in a collection or an attribute of an object.
ndc85430 and buran like this post
Reply
#3
(Dec-29-2021, 07:06 PM)deanhystad Wrote: This is not something you should ever need to do. That you think you need something like this is a flashing neon sign that says "This is a BAD design!"

Why do you have a bunch of variables that you want to collect into a list? If you have things that should be collections, they should be created as collections. Variables for individual objects should be fairly rare in programs, usually existing for fleeting moments in time where they are used to represent an item in a collection or an attribute of an object.

I'm sorry but you're overthinking this. I asked out of curiosity, because I was playing around with things. I'm not designing anything. I'm just checking what's possible and what's not.
Reply
#4
You can do anything in Python. Python reflects everything, so programs can snoop out which variables and which functions and which methods are available. You can write functions that rewrite themselves during runtime for heavens sake. But there are also things you should probably not do in Python. You should not write programs that have lots of variables that hold related information, you should use one of the collection variable types instead (dictionary, list, tuple, set...).
ndc85430 likes this post
Reply
#5
(Dec-29-2021, 08:06 PM)deanhystad Wrote: You can do anything in Python. Python reflects everything, so programs can snoop out which variables and which functions and which methods are available. You can write functions that rewrite themselves during runtime for heavens sake. But there are also things you should probably not do in Python. You should not write programs that have lots of variables that hold related information, you should use one of the collection variable types instead (dictionary, list, tuple, set...).

I understand what you're saying, but it doesn't change the fact that i want to know how. Curiosity is not the same as efficiency
Reply
#6
Why do you want to know how to do things badly?
Reply
#7
Without a reason it is difficult answer your question. A module can ask for it's filename and even get hold of it's own code. You could then parse the code to learn everything you want. But that would be hard and I don't know why you would ever want to do it.

You could use locals() to get a dictionary of all the variables that exist in the current scope/namespace. You could parse the keys to select variables to collect in you list. You could use dir() to get similar info in the form of a list.

The statement "There are no stupid questions" is a lie. This is a stupid question. It is stupid because it is pointless. If you can think of a reason why you might want to collect a bunch of variables into a collection it ceases being pointless and stupid. If you post the reason why you want to know this (other than curiosity) it provides a context that will help others understand and answer your question. "I want to open up a window that shows me the values of all my program variables. How could I do that?" is not a stupid, pointless question. Do you have a question like that?
Reply
#8
(Dec-29-2021, 06:52 PM)CompleteNewb Wrote: I know you can do it manually, like i just did, but is there a way to automate it?
N.B: i gave an exemple for variables, but it can be any items, even objects.

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?
Reply
#9
(Dec-29-2021, 09:22 PM)deanhystad Wrote: Without a reason it is difficult answer your question. A module can ask for it's filename and even get hold of it's own code. You could then parse the code to learn everything you want. But that would be hard and I don't know why you would ever want to do it.

You could use locals() to get a dictionary of all the variables that exist in the current scope/namespace. You could parse the keys to select variables to collect in you list. You could use dir() to get similar info in the form of a list.

The statement "There are no stupid questions" is a lie. This is a stupid question. It is stupid because it is pointless. If you can think of a reason why you might want to collect a bunch of variables into a collection it ceases being pointless and stupid. If you post the reason why you want to know this (other than curiosity) it provides a context that will help others understand and answer your question. "I want to open up a window that shows me the values of all my program variables. How could I do that?" is not a stupid, pointless question. Do you have a question like that?

You know... I just wanted to know if there was an easy way to make a list out of previously declared ITEMS, and you could have givin me an example to show me that it would not be worth the effort, and I would have thanked you and moved on. Problem is that you decided that I shouldn't learn that, because it didn't fit your own frame of reference. That is very self centered and you should know that it's not because you don't understand the purpose of a question that it is a stupid question. There are no stupid question! If a "learner" thinks a question is relevant, it's relevant. It's not up to you to decide . If you spent more time doing things you're not good at, you would understand
Reply
#10
(Dec-29-2021, 09:33 PM)bowlofred Wrote:
(Dec-29-2021, 06:52 PM)CompleteNewb Wrote: I know you can do it manually, like i just did, but is there a way to automate it?
N.B: i gave an exemple for variables, but it can be any items, even objects.

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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 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,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Remove an item from a list contained in another item in python CompleteNewb 19 5,552 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,373 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Create SQLite columns from a list or tuple? snakes 6 8,521 May-04-2021, 12:06 PM
Last Post: snakes
  Time.sleep: stop appending item to the list if time is early quest 0 1,847 Apr-13-2021, 11:44 AM
Last Post: quest
  Create variable and list dynamically quest_ 12 4,288 Jan-26-2021, 07:14 PM
Last Post: quest_
  How to run a pytest test for each item in a list arielma 0 2,325 Jan-06-2021, 10:40 PM
Last Post: arielma
  How to create a linked list and call it? loves 12 4,359 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