Lists, Python Programming


Python has  a built-in list data  structure that  is easy  to use  and  incredibly convenient.  So, for that point, you can say

>>> y = [1, 2, 3]

>>> y[0]


>>> y[2]


>>> y[-1]


>>> y[-2]


>>> len(y)


>>> y + [4]

[1, 2, 3, 4]

>>> [4] + y

[4, 1, 2, 3]

>>> [4,5,6] + y

[4, 5, 6, 1, 2, 3]

>>> y

[1, 2, 3]


A list is written using  square brackets, with  whole code separated by commas. You may get components out  by specifying the  index  of the  element you  want  in square brackets, but  note  that,  like for character or string, the indexing initiate with  0.  Note  that  you  can index  elements of a list starting from  the initial (or zeroth) one (by using integers), or initiating from the last one (by using negative integers). You can add elements to a list using '+', taking  advantage of Python operator overloading. Note that this method does not modify  the original list, but creates  a new one.

Another useful  thing  to know  about  lists is that  you can make  slices of them.  A part of a structure is sublist;  you may get the basic idea from examples.


>>> b = range(10)

>>> b

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b[1:]

[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b[3:]

[3, 4, 5, 6, 7, 8, 9]

>>> b[:7]

[0, 1, 2, 3, 4, 5, 6]

>>> b[:-1]

[0, 1, 2, 3, 4, 5, 6, 7, 8]

>>> b[:-2]

[0, 1, 2, 3, 4, 5, 6, 7]


Posted Date: 8/9/2012 7:20:13 AM | Location : United States

Related Discussions:- Lists, Assignment Help, Ask Question on Lists, Get Answer, Expert's Help, Lists Discussions

Write discussion on Lists
Your posts are moderated
Related Questions

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

Coding examples Following are some  attempts at defining a function isSubset,  which  takes  two  arguments, a and  b, and  returns True if a is a subset  of b, assuming that

Procedures in Firrst-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variabl

Java programs are  what  is known as statically and  strongly defined.  Thus,  the  kinds  of all the variables must  be known at the time that the program is written. That seems

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

De?nition   A method de?nition has the abstract form: def ( , ..., ): ...   There are essentially three parts: is a name for

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e