List is the basic Container
type in Python. A List in Python is a Ordered list of Objects. These objects
can be numbers, Strings and so on. List is a Sequence of Data and list is a
structure which can be changed.
One important thing is those
python lists are mutable objects that can be changed in place.
Some of the features of List in
Python are –
Ordered
Collection – The List can hold other types of objects thus helping them to be
managed as a group. Python list are a sequence
which means they maintain a left to right positional ordering just like we saw
in strings.
Objects can be accessed by
indexing
List in Python are mutable
object thus allowing things to be changed in place
Creating a List – A List in
Python can be created by
b = ['abc', 123, ['def', 456],
{}]
See a List can contain
different types of objects like string , number and also other Container types
like Tuple or Set etc.
In order to create a Empty
List, we use
>>> a = []
>>> print a
[]
>>> b = list()
>>> print b
[]
List() is a function available
in Python by which we can create a List or by using “[]”
We can use the list() function
available and can be used as
>>>
list('Mississippi')
['M', 'i', 's', 's', 'i', 's',
's', 'i', 'p', 'p', 'i']
Iteration – Iterating
through the Python list can be done as,
>>> a = [1, 2, 3]
>>> for var in a:
... print(var)
...
1
2
3
Concatenating –
Python provides Concatenating feature for List. We can do a Concatenation in
Python using
>>> a = [1, 2, 3, 4,
5]
>>> b = [6, 7, 8, 9,
10]
>>> a + b
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Repetition – We
can use multiplication Operator if we want to do a repetition of List as
>>> a = [1, 2, 3, 4,
5]
>>> a * 2
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Indexing – Indexing allows to
access the List objects by index as,
>>> a = ['List',
12345, [123, 456]]
>>> a[1]
12345
We can use a negative value if
we want to access the List from last, like
>>> a = ['List',
12345, [123, 456]]
>>> a[-1]
[123, 456]
Length – The Length of the List
can be obtained using len() funcation as,
>>> len([1,2,3,4,5])
5
There are 2 important functions
for List and they are extend and append
append: Appends object at end
extend: extends list by appending elements to the
existing ones
x = [1, 2, 3]
x.append([4, 5])
x.append([4,5])
print (x) -
append - [1, 2, 3, [4, 5]]
extend – [1,2,3,4,5]
Deletion – A Element in the list can be
deleted as
list1 = ['physics',
'chemistry', 1997, 2000];
del list1[2];
Now the element 2 from the
List1 will be deleted.
Slice - A slice extracts elements,
based on a start and stop. in Slicing we specify an optional first index, an
optional last index, and an optional step. When we omit a value, a default is
used. The Syntax for the slicing includes,
values[1:3] Index 1 through index 3.
values[2:-1] Index 2 through
index one from last.
values[:2] Start through index 2.
values[2:] Index 2 through end.
values[::2] Start through end, skipping ahead 2 places
each time.
A Example of List Slicing is,
>>> numbers = [1, 2,
3, 4, 5, 6, 7, 8, 9, 10]
>>> print numbers[3:6]
[4, 5, 6]
>>> print numbers[0:1]
[1]
>>> print
numbers[7:10]
[8, 9, 10]
>>> print numbers[-3:-1]
[8, 9]
>>> print numbers[-3:]
[8, 9, 10]
>>> print numbers[:3]
[1, 2, 3]
>>> print numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Nested List –
Python Supports Nested Lists as
>>> q =[2,3]
>>> p =[1,q,4]
>>> print len(q)
2
>>> print p[1]
[2, 3]
>>> print p[1][0]
2
Reversing -
Reversing is fast, so temporarily reversing the list can often speed things up
if you need to remove and insert a bunch of items at the beginning of the
list: list.reverse()
Some more important methods in
Python are,
list.append(object)
list.count(object)
list.extend(sequence)
list.index(object)
list.pop([index])
list.remove(object)
No comments :
Post a Comment