Link to home
Create AccountLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

pyhton list x = [ ] vs mylist x = [ ]

Hi there;

I am creating lists in python and I am using komodo.

Can someone tell me whether the following are equivalent?

list x = [ ]  vs. mylist x = [ ]

The reason is that when I input list, komodo highlights it in orange and when I input mylist, it's simple black.

Any fundamental difference in these?

Regards.
Avatar of pepr
pepr

Both commands are wrong. You simply create new list like this:
x = []

Open in new window

You can alternatively use
x = list()

Open in new window

but it is not prefered when creating the empty list.

The reason why the list is highlighted is that it is a built-in type in Python (one of the most frequently used containers, used as a list, as a stack, as a queue, as an array). You can try in the interactive mode (wrapped lines by the console):
c:\tmp\___python\jazzIIIlove>py
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = list()
>>> x
[]
>>> list
<type 'list'>
>>> list.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit
em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
 '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__'
, '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a
ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
]
>>>

Open in new window

The mylist is just some identifier. But in Python, you do not tell the data type when creating a variable. Variables are not bound to any type. They are just names given to references. The reference values are not bound to the target type either. They are untyped.
Avatar of jazzIIIlove

ASKER

Ok, sorry for my typo.

So list and mylist are same in this context. Right?

MvH.
Yes. They are both wrong in the context. Syntax highlighting in editors just highlights certain character sequences. It knows the list, and it does not know the mylist. This is the only reason for the different colours.
Ok, don't get mad.

list = [ ]  and mylist = [ ] are same. Right?

Regards.
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hey, this was my 1000. question in EE. :)
Good questions are often better than good answers. The questions make us thinking. When the question is not known, the anwer cannot be found. Also, we often ignore the answers if we do not understand the questions. :)