FongYu
FongYu

Reputation: 767

Referencing Python list elements without specifying indexes

How can I store values in a list without specifying index numbers?

For example

outcomeHornFive=5

someList = []
someList.append(outComeHornFive)

instead of doing this,

someList[0] # to reference horn five outcome

how can i do something like this? The reason is there are many items that I need to reference within the list and I just think it's really inconvenient to keep track of which index is what.

someList.hornFive

Upvotes: 1

Views: 5967

Answers (4)

jsj
jsj

Reputation: 9421

Instead of using a list you can use a dictionary.

See data types in the python documentation.

A dictionary allows you to lookup a value using a key:

my_dict["HornFive"] = 20

Upvotes: 3

FongYu
FongYu

Reputation: 767

referenced from http://parand.com/say/index.php/2008/10/13/access-python-dictionary-keys-as-properties/

Say you want to access the values if your dictionary via the dot notation instead of the dictionary syntax. That is, you have:

d = {'name':'Joe', 'mood':'grumpy'}

And you want to get at “name” and “mood” via

d.name d.mood

instead of the usual

d['name']
d['mood']

Why would you want to do this? Maybe you’re fond of the Javascript Way. Or you find it more aesthetic. In my case I need to have the same piece of code deal with items that are either instances of Django models or plain dictionaries, so I need to provide a uniform way of getting at the attributes.

Turns out it’s pretty simple:

class DictObj(object):
    def __init__(self, d):
        self.d = d

    def __getattr__(self, m):
        return self.d.get(m, None)

d = DictObj(d)
d.name
# prints Joe
d.mood
# prints grumpy

Upvotes: 0

user395760
user395760

Reputation:

You cannot and you shouldn't. If you could do that, how would you refer to the list itself? And you will need to refer to the list itself.

The reason is there are many items that i need to reference within the list and I just think it's really inconvenient to keep track of which index is what.

You'll need to do something of that ilk anyway, no matter how you organize your data. If you had separate variables, you'd need to know which variable stores what. If you had your way with this, you'd still need to know that a bare someList refers to "horn five" and not to, say, "horn six". One advantage of lists and dicts is that you can factor out this knowledge and write generic code. A dictionary, or even a custom class (if there is a finite number of semantically distinct attributes, and you'd never have to use it as a collection), may help with the readability by giving it an actual name instead of a numeric index.

Upvotes: 0

Julian
Julian

Reputation: 3429

You can use another data structure if you'd like to reference things by attribute access (or otherwise via a name).

You can put them in a dict, or create a class, or do something else. It depends what kind of other interaction you want to have with that object.

(P.S., we call those lists, not arrays).

Upvotes: 3

Related Questions