Skip to main content

LISTS in Python

One of the basic data structures in Python is sequences. There are six built-in types of sequence - strings, Unicode strings, lists, tuples, buffers, and xrange objects. The most common are lists and tuples.
 A list is an ordered set of values, where each value is identified by an index. The values in the list are known as elements. Each element of a sequence is assigned with a number - its position or index. The first index is zero, second is one and so forth.

Creation and Accessing List:
The list can be created by putting values separated by commas between square brackets []. List is one of the most freuently used and very versatile data type used in Python. 

Syntax for List:

               <List_name> = [<Value_1>,<Value_2>,........,<Value_N>]

NOTE: Nested lists are also acceptable.


List Operations:

Two operations + and * is allowed in list. + for concatenation of lists and * repeats the list elements a given number of times.

Try Yourself:
Nos_List = [1,2,3]
Char_List = ["a","b"]
#add two lists
Add_list = Nos_list + Char_List
#Append same elements n no.of times
Repeat_List = Char_List * 3

print "Add_List : ", Add_List
print "Repeat_List : ", Repeat_List

List Slices:

A subsets of elements of a list is known as slice of list. Selecting a list slice is similar to selecting a character in a string.


Try Yourself:
Nos_list = [1,2,3,4,5,6,7,8,9,10,11]

#different slicing of list
print "Elements 3rd to 5th : ", Nos_list[2:5]
print " Elements beginning up to -5th index : ", Nos_list[:-5]
print "Elements from 5th to end : ", Nos_list[5:]
print "Elements from beginning to end : ", Nos_list[:]

List Methods:

The list datatypes has some methods. Methods that are available for the list object in Python Programming are tabulated as follows:

The following table gives the result of every method for the considered list. in general the methods are accessed as ListVariable.MethodName().

 S.No
 Method 
 Description
 1.
append(E) 
Add a an element(E) to the end of the list. 
 2.
extend(seq) 
All elements of the list to another list(seq).
 3.
insert(ind,E) 
Insert an element(E) at the defined index(ind).
 4.
remove(E) 
Removes the first occurrence of the element (E) from the list. 
 5.
pop(ind) 
Removes and returns the element at the given index(ind).
 6.
index(E) 
Returns the index of the first matched element with E.
 7.
count(E) 
Returns the count of number of elements (E) in the list. 
 8.
sort( ) 
Sort items in a list in ascending order. 
 9.
reverse( ) 
Reverse the order of items in the list. 
 10.
copy( ) 
Returns the shallow copy of the list. 




Comments

Post a Comment

Popular posts from this blog

Python NumPy Introduction

Python is emerging as one of the favorite tools in the filed of data science. With powerful data science libraries like NumPy, SciPy, pandas, matplotlib, scikit-learn and tools like IPython (Jupyter) notebook combined with ease of programming, Python is proven as the powerful and preferred language of organizations. In this course I will tech you the basics of NumPy and further take a deep dig on playing with NumPy. NumPy  : NumPy is a python library, which supports efficient handling of various numerical operations on arrays holding numeric data.  They are known as N-dimensional-arrays or ndarrays.  Ndarrays are capable of holding data elements in multiple dimensions  and each data element of it is of fixed size and also all the elements of ndarray are of same data type. N-dimensional array  ( ndarray)  : N-dimensional array is an object, capable of holding data elements of same type and of fixed size in multiple dim...

5 Kind of Bugs Every Programmer Encounter During Coding

                As a programmer, you have to expect bugs. In simple terms, a bug can be defined as an error in a program. During the coding of a program, we often make some mistakes. These mistakes showcase themselves as bugs in your code. Writing a code is the easy part. The hard step is the debugging (Searching for the errors or bugs in a program). And it can get especially frustrating in situations where you create more bugs instead of fixing the current one. If you haven’t encountered the following bugs, you should expect them any time soon: Tiny Bugs:                 These types of bugs may be miniature, but dealing with them is no easy task. You will receive compiler errors, and then spend hours, or even days trying to figure out where you went wrong. Such bugs include forgetting that little semicolon or bracket. In a programming language like Python, you can face trouble when indentation i...

Python File

File handling is the most important part of any Web Applications. This has several functions for creating, reading, updating, deleting files etc., File Handling:           The key for working with files in Python is the open() function. Syntax: open(filename, mode) There are four different methods for opening a file: "r" - Read - Default value. Opens a file for reading, error if the file does not exist. "a" - Append - Opens a file for appending, creates the file if it does not exist. "w" - Write - Opens a file for writing, creates the file if it does not exist. "x" - Create - Creates the specified file, returns an error if the file exists. In addition you can specify if the file should be handled as binary or text mode: "t" - Text - Default value. Text mode. "b" - Binary - Binary mode(e.g., images) Note: Make sure the file exists, or else you will get an error. Syntax: f = ...