Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9109733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:06:52+00:00 2026-06-17T03:06:52+00:00

I have multiple variables that I need to pack as one and hold it

  • 0

I have multiple variables that I need to pack as one and hold it sequentially like in a array or list. This needs to be done in Python and I am still at Python infancy.

E.g. in Python:

a = Tom
b = 100
c = 3.14
d = {'x':1, 'y':2, 'z':3}

All the above in one sequential data structure. I can probably try and also a similar implementation I would have done in C++ just for the sake of clarity.

struct
{ 
string a;
int b;
float c;
map <char,int> d;// just as an example for dictionary in python
} node;

vector <node> v; // looking for something like this which can be iterable

If some one can give me a similar implementation for storing, iterating and modifying the contents would be really helpful. Any pointers in the right direction is also good with me.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:06:53+00:00Added an answer on June 17, 2026 at 3:06 am

    You can either use a dictionary like Michael proposes (but then you need to access the contents of v with v['a'], which is a little cumbersome), or you can use the equivalent of C++’s struct: a named tuple:

    import collections
    
    node = collections.namedtuple('node', 'a b c d')
    
    # Tom = ...
    
    v = node(Tom, 100, 3.14, {'x':1, 'y':2, 'z':3})
    print node  # node(a=…, b=100, c=3.14, d={'x':1, 'y':2, 'z':3})
    print node.c  # 3.14
    print node[2]  # 3.14 (works too, but is less meaningful and robust than something like node.last_name)
    

    This is similar to, but simpler than defining your own class: type(v) == node, etc. Note however, as volcano pointed out, that the values stored in a namedtuple cannot be changed (a namedtuple is immutable).

    If you indeed need to modify the values inside your records, the best option is a class:

    class node(object):
        def __init__(self, *arg_list):
            for (name, arg) in zip('a b c d'.split(), arg_list):
                setattr(self, name, arg)
    
    v = node(1, 20, 300, "Eric")
    print v.d  # "Eric"
    v.d = "Ajay"  # Works
    

    The last option, which I do not recommend, is indeed to use a list or a tuple, like ATOzTOA mentions: elements must be accessed in a not-so-legible way: node[3] is less meaningful than node.last_name; also, you cannot easily change the order of the fields, when using a list or a tuple (whereas the order is immaterial if you access a named tuple or custom class attributes).


    Multiple node objects are customarily put in a list, the standard Python structure for such a purpose:

    all_nodes = [node(…), node(…),…]
    

    or

    all_nodes = []
    for … in …:
        all_nodes.append(node(…))
    

    or

    all_nodes = [node(…) for … in …]
    

    etc. The best method depends on how the various node objects are created, but in many cases a list is likely to be the best structure.

    Note, however, that if you need to store something akin to an spreadsheet table and need speed and facilities for accessing its columns, you might be better off with NumPy’s record arrays, or a package like Pandas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ingredients array which contains multiple variables (like name, icon, value etc.).
I have a text that has multiple occurrences of variables in this format: Example
I have a set of variables that i need for multiple other classes. I
The situation: I have multiple classes that should each hold a variable with a
I have text documents like the following which contain single and multiple variables: title::
I have 2 lists, each with multiple variables (I think this is the correct
So I need a linked list to store multiple variables, so I want to
I have a multiple classes each with different member variables that are initialized trivially
I have multiple projects that need to pass user login information to common-api project
I had a question about Java Class Path variables. If I have multiple jars

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.