2

I am new to python and i was just reading up about lists. I have been trying to find out if a list is a variable

e.g. Hello = [] 

This is because I read that you assign a variable by using the '=' sign. Or, am I just assigning the empty list a name in the example above.

4 Answers 4

2

No. A list is an object. You assign a list to a name-reference with =.

Thus a = [1,2] produces a which is a name-reference (a pointer essentially) to the underlying list object which you see by looking at globals().

>>> a = [1,2] >>> globals() {'a': [1, 2], '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None} 

A list is an instance of a ListType, which is a subclass of an object.

>>> import types >>> types.ListType.mro() [<type 'list'>, <type 'object'>] >>> object <type 'object'> >>> b = types.ListType() >>> b [] 
Sign up to request clarification or add additional context in comments.

Comments

1

In Python, the concept of object is quite important (as other users might have pointed out already, I am being slow!).

You can think of list as a list (or actually, an Object) of elements. As a matter of fact, list is a Variable-sized object that represents a collection of items. Python lists are a bit special because you can have mixed types of elements in a list (e.g. strings with int)But at the same time, you can also argue,"What about set, map, tuple, etc.?". As an example,

>>> p = [1,2,3,'four'] >>> p [1, 2, 3, 'four'] >>> isinstance(p[1], int) True >>> isinstance(p[3], str) True >>> 

In a set, you can vary the size of the set - yes. In that respect, set is a variable that contains unique items - if that satisfies you....

In this way, a map is also a "Variable" sized key-value pair where every unique key has a value mapped to it. Same goes true for dictionary.

If you are curious because of the = sign - you have already used a keyword in your question; "Assignment". In all the high level languages (well most of them anyway), = is the assignment operator where you have a variable name on lhs and a valid value (either a variable of identical type/supertype, or a valid value).

Comments

0

The line:

Hello = [] 

creates a new, empty list object (instance of the list class), and assigns a reference to that object to the name (or "identifier") Hello.

You can then access that list object via the name Hello (as long as it is accessible in the current scope), e.g.:

Hello.append('World') 

Lists are mutable, i.e. they can be changed in-place, in this case by appending (adding) a new string object to the list referenced by the name Hello. This may be what you meant by "variable".

For more on names in Python, see http://nedbatchelder.com/text/names.html.

For more on lists (and Python's other built-in sequence types), see the official docs.

Comments

0

Python lists are Data structure which enables you to hold a collection of data items. Data structures are objects or programming types which enable you to store any kind of data been integer, string etc. in memory or permanently on hard disk.

In your case, you have defined a list structure - which will store collection of data items referenced by the symbol hello. The symbol hello is termed as variable in programming.

Using a variable, you can reference your list from any part of your program to locate and access its member items.

Example:

hello = [1, 3, 100] 

Calling hello with its Nth index starting at 0 will access and return the value placed within the Nth index location.

print hello[0] 

Which will output 1 .


See Array data structure for more examples.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.