Python List:
A list is a collection of elements which is ordered and changeable.
List in python is mutable.
In Python lists are written with square brackets.
Example:
list1 = ["book", "copy", "pen"]
print(list1)
Output:
["book", "copy", "pen"]
Accessing Items in list:
In list we can access items with the help of their index number.
The indexing of list start from zero.
Example:
list1 = ["book", "copy", "pen"]
print(list1[1])
Output:
["copy"]
Note:The index number of list should put in square bracket.In above example we can see that at [1] index "copy" is present which means that the indexing of list start from 0.
Note:The index number of list should put in square bracket.In above example we can see that at [1] index "copy" is present which means that the indexing of list start from 0.
Using of For Loop in List:
Example:
list1 = ["book", "copy", "pen"]
for x in list1[1]:
print(x)
print(x)
Output:
book
copy
pen
copy
pen
Negative Indexing of list:
Negative indexing means beginning from the end
Example:
Negative indexing means beginning from the end
-1
and -2
refers to the second last item and so on.Example:
list1 = ["book", "copy", "pen"]
print(list1[-1])
Output:
["pen"]
Specifying Range of Indexes:
Example:
list1 = ["book", "copy", "pen", "scale", "pencil"]
list1 = ["book", "copy", "pen", "scale", "pencil"]
print(list1[1:4])
Output:
["copy", "pen", "scale"]
#This will return the items from position 1 to 4.
#Similarly,we can do it for negative index.
Example:
list1 = ["book", "copy", "pen", "scale", "pencil"]
#This example returns the items from index -4 which is included and index -1 is excluded.
#Remember that the last item has the index -1.
Example 1:
Output:
[ ]
#Remember that the first item is position 0,and note that the item in position 5 is not included
#Similarly,we can do it for negative index.
Example:
list1 = ["book", "copy", "pen", "scale", "pencil"]
print(list1[-4:-1])
Output:
["copy", "pen", "scale"]
#Remember that the last item has the index -1.
Changing Item in list:
Example:
list1 = ["book", "copy", "pen"]
list1[0] = "scale"
print(list1)
Output:
["scale", "copy", "pen"]
Checking Item exists in list or not:
To determine if a item is present in a list we use the
in
keyword:
Example:
list1 = ["book", "copy", "pen"]
if "copy" in list1:
print("Yes, 'copy' is in the list")
Output:
Yes, 'copy' is in the list
Length of list:
To determine how many items are present in list we use the
len()
method:
Example:
list1 = ["book", "copy", "pen"]
print(len(list1))
Output:
3
Adding Items in list:
To add an item to the end of the list, use the append() method:
Example:
Example:
list1 = ["book", "copy", "pen"]
list1.append("scale")
print(list1)
Output:
["book", "copy", "pen", "scale"]
Inserting Items in list:
To add an item at particular index we use the insert() method:
Example:
list1 = ["book", "copy", "pen"]
list1.insert(1, "scale")
print(list1)
Output:
["book", "scale", "copy", "pen"]
Remove Item from list:
To remove an item from list we use the remove() method:
Example:
list1 = ["book", "copy", "pen", "scale"]
list1.remove("scale")
print(list1)
Output:
["book", "copy", "pen"]
pop( ) method for list:
pop() method remove the particular index item or last item if index is not specified.
Example:
list1 = ["book", "copy", "pen", "scale"]
list1.pop()
print(list1)
Output:
["book", "copy", "pen"]
del( ) method for list:
Example 1:
list1 = ["book", "copy", "pen", "scale"]
list1.del[3]
print(list1)
Output:
["book", "copy", "pen"]
Example 2:
list1 = ["book", "copy", "pen", "scale"]
del (list1)
print(list1)
Output:
# This will cause error as list has been deleted.
clear( ) method for list:
clear() method clear the whole list.
Example:
list1 = ["book", "copy", "pen", "scale"]
list1.clear()
print(list1)
[ ]
Copying a list into another:
copy() method copy the list.
Example:
list1 = ["book", "copy", "pen", "scale"]
list2 = list1.copy()
print(list2)
Output:
["book", "copy", "pen", "scale"]
count( ) method for list:
count() method count the element repetition in list.
Example:
list1 = ["book", "copy", "pen", "scale", "pen"]
x = list1.count("pen")
print(x)
Output:
2
extend( ) method in list:
index( ) method in list:
x = list1.index("pen")
print(x)
sort() method for list:
["book", "copy", "pen"]
list( ) Constructor:
list1 = list(("pen", "copy", "book"))
["pen", "copy", "book"]
extend( ) method in list:
extend() method add one list to the another list.
Example:
list1 = ["book", "copy", "pen"]
list2 = ["scale", "pen"]
list2 = ["scale", "pen"]
list1.extend(list2)
print(x)
Output:
["book", "copy", "pen", "scale", "pen"]
index( ) method in list:
index() method returns the position of element in list.
Example:
list1 = ["book", "copy", "pen", "scale"]x = list1.index("pen")
print(x)
Output:
2
2
reverse( ) method in list:
reverse() method returns the position of element in list.
Example:
list1 = ["book", "copy", "pen"]
list1.reverse("pen")
print(list1)
Output:
["pen", "copy", "book"]
sort() method for list:
sort()
method sorts the list in ascending order by default.
Example:
list1 = ["pen", "copy", "book"]
list1.sort()
print(list1)
Output:
list( ) Constructor:
list() constructor to make a new list.
Example:
list1 = list(("pen", "copy", "book"))
print(list1)
Output:
0 Comments