List in Python



A list is an Python-object which can contain many different types of elements inside it like integer, characters or array and even another list inside it. Lists are sometimes called recursive, because a list can contain other lists. This makes them fundamentally different from others.

Syntax
lst = []






Code Snippet

languages = ["C", "C++", "Perl", "Python"]
for letter in languages:
 print letter

 

Note: we can also delete element form the list using “del” command with index
del languages[1]
for letter in languages:
 print letter

List Manipulation functions

cmp(list1, list2)     Comparing the two list A negative number if list1 is less than
                             list2 or Zero if list1 is equal to list2 or A positive number
if list1 is greater than list2.

len(list)                 Returns the length of the list
list.count(x)           Returns the number of occurrences of x in list
list.index(x)           Returns the index of the first occurrence of x in list 
list.append(x)        Appends x to the end of list
list.extend(l)         Appends all the items of list l to the end of list
list.insert(i,x)        Inserts x at index i in list
list.remove(x)        Removes the first occurrence of x from list
list.pop(i=-1)         Returns the value of the item at index i and removes it from list
list.reverse( )        Reverses, in-place, the items of list
list.sort(f=cmp)     Sorts, in-place, the items of list, comparing items by f
List in Python Reviewed by Rupesh on 09:24 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.