A list is an ordered sequence of values, where each value is identified by an index. The first index is zero and next is one and further in sequential order. The syntax for expressing literals of type list is []. The empty list is written as [], and singleton lists are written without comma before the closing bracket like [4].
Difference between tuples and lists:
Some basic operations done on the following 2 list are described below:
L1 = [1, 2,3,4 ]
L2 = [5, 6, 7, 8]
Concatenation
print L1 + L2 will give the print:
[1, 2, 3, 4, 5, 6, 7, 8]
Repetition
print L1 * 2 will give the print:
[1, 2, 3, 4, 1, 2, 3, 4]
Find length
print len(L1) will give the length of list L1:
for n in L1: print n
1 2 3 4
Indexing
Any particular element can be reached through indexing
print L1[2]:
will print 3rd element of List L1 as indexing always starts from zero.
Slicing
print L1[1:3]
will slice oth element and print 1st , 2nd element and then again slice.
So output will be
[1, 2 ]
Methods associated with lists:
list.append(a) : adds object a to the end of list
list.sort() : sorts the elements of list in ascending order
list.extend(L1): adds the items in list L1 to end of list
list.count(e) : returns the number of times e occurs in list
list.index(e) : returns the index of the first occurrence of e in list.
list.insert(i, e) : inserts object e at index i
list.pop(i) : removes and returns the item at index i in L
List comprehension
This mechanism provides a concise way to apply an operation to the values in a sequence. It creates a new list in which each element is the result o applying a given operation to a value from a sequence(e.g. the elements in another list)
For example,
L =[x**2 for x in range(1,7)]
print L
will print the list :
[1, 4, 6, 9, 16, 25, 36]
The for clause in a list comprehension can be followed by one or more if and for statements that are applied to the values produced by the for clause. These additional clauses modify the sequence of values generated by the first for clause and produce a new sequence of values, to which the operation associated with the comprehension is applied.
For example,
mixed = [1, 2, 'a' , 3, 4.0]
print [x**2 for x in mixed if type(x) == int]
which will give the output
[1, 4, 9]
Difference between tuples and lists:
- Lists are defined using square brackets
- For singleton lists , is not used
- The most important property is lists are mutable whereas tuples and strings are immutable. Objects of mutables type can be modified after they are created.
Some basic operations done on the following 2 list are described below:
L1 = [1, 2,3,4 ]
L2 = [5, 6, 7, 8]
Concatenation
print L1 + L2 will give the print:
[1, 2, 3, 4, 5, 6, 7, 8]
Repetition
print L1 * 2 will give the print:
[1, 2, 3, 4, 1, 2, 3, 4]
print len(L1) will give the length of list L1:
4
Iteration for n in L1: print n
1 2 3 4
Indexing
Any particular element can be reached through indexing
print L1[2]:
will print 3rd element of List L1 as indexing always starts from zero.
Slicing
print L1[1:3]
will slice oth element and print 1st , 2nd element and then again slice.
So output will be
[1, 2 ]
Methods associated with lists:
list.append(a) : adds object a to the end of list
list.sort() : sorts the elements of list in ascending order
list.extend(L1): adds the items in list L1 to end of list
list.count(e) : returns the number of times e occurs in list
list.index(e) : returns the index of the first occurrence of e in list.
list.insert(i, e) : inserts object e at index i
list.pop(i) : removes and returns the item at index i in L
List comprehension
This mechanism provides a concise way to apply an operation to the values in a sequence. It creates a new list in which each element is the result o applying a given operation to a value from a sequence(e.g. the elements in another list)
For example,
L =[x**2 for x in range(1,7)]
print L
will print the list :
[1, 4, 6, 9, 16, 25, 36]
The for clause in a list comprehension can be followed by one or more if and for statements that are applied to the values produced by the for clause. These additional clauses modify the sequence of values generated by the first for clause and produce a new sequence of values, to which the operation associated with the comprehension is applied.
For example,
mixed = [1, 2, 'a' , 3, 4.0]
print [x**2 for x in mixed if type(x) == int]
which will give the output
[1, 4, 9]
No comments:
Post a Comment