are these operations on list valid?

Tim Daneliuk tundra at
Sun Oct 6 09:40:07 CEST 2002

Ken wrote:
> Hi, just like to know if these are valid:
> 1:
> Use:
> var1 = "hello"
> var2 = "world"
> list = [var1, var2]
> Instead of:
> list = []
> list.append(var1)
> list.append(var2)
> --------------------

Yes, this will work.  BTW, the easy way to do this is to try it with
the interpreter - go to the command line of your system and type:


And hit Enter.  You can then enter the commands you would like to try.

> 2.
> Use:
> list=["hello", "world"]
> if list==["hello", "world]: #comparing list with list without examining
> through every element inside
>   do something.....
> Instead of:
> list1=["hello", "world"]
> if list[0]==list1[0] and list[1]==list1[1]:
>   do something....
> ---------------------

Yup, this should work too.  What you are doing is comparing two *objects*
for equality.  There is some subtlety to this, however, because objects
can contain other objects which can contain other objects... and so on.
What makes two objects "equal" can therefore be a bit tricky.  Before
doing this sort of thing widely, you probably should do a bit of reading
on the subjects if object equality and identity.  A Google search will
yield quite a bit on the topic...

Tim Daneliuk
tundra at

More information about the Python-list mailing list