Python Forum
Keyword to build list from list of objects?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyword to build list from list of objects?
#1
Question 
Hi,

    # Code borrowed from geeksforgeeks.com

    class geeks:
        def __init__(self, name, roll):
            self.name = name
            self.roll = roll

    # creating list
    list = []

    # appending instances to list
    list.append(geeks('Akash', 2))
    list.append(geeks('Deependra', 40))
    list.append(geeks('Reaper', 44))

    # Is there a single instruction to do this?
    namelist = []
    for obj in list:
        namelist.append(obj.name)

    print (namelist)
The above traverses the object list to build a list of one of the parameters of this list.

Is there a built in command that does that? Think

Thanks
Larz60+ write Aug-06-2022, 10:43 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
A list comprehension: https://docs.python.org/3/tutorial/datas...rehensions.
Reply
#3
Don't use list as a variable name. It overwrites the Python object.

alist.append(geeks('Akash', 2))
namelist.append(alist[-1].name)

## or
for name, num in ((Akash', 2), ('Deependra', 40), ('Reaper')):
    alist.append(geeks(name, num))
    namelist.append(name)
Reply
#4
What the first reply said.

data = [('Akash', 2), ('Deependra', 40), ('Reaper', 44)]
names = [tup[0] for tup in data]
But wouldn't you normally have names and numbers stored in an Excel, csv or database? What is that class for?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't list require global keyword? johnywhy 9 796 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Find a specific keyword after another keyword and change the output sgtmcc 5 808 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,157 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,612 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Split a number to list and list sum must be number sunny9495 5 2,279 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to store the resulting Doc objects into a list named A xinyulon 1 1,894 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  How to check if a list is in another list finndude 4 1,836 Jan-17-2022, 05:04 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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