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:
No comments: