3. Python Lists:
Lists
are
the most versatile of Python's compound data types. A list contains items
separated by commas and enclosed within square brackets ([]).
To
some extent, lists are similar to arrays in C. One difference
between them is
that all the items belonging to a list can be of
different data type i.e., all the items in a list do not need to be
of the same type.
# Empty List
my_list = []
# list of integers
my_list = [1,2,3,4]
# list with mixed data types
my_list = [5,"Hey",15.4]
Also, a list can even have another list as an item. This is called
nested list.
# nested list
my_list = [ "micky", [2,0,9], [ 'n' ]]
The
values stored in a list can be accessed using the slice operator
([ ] and [:])
with indexes starting at 0 in the beginning of the list
and working their way
to end -1. The plus (+) sign is the list
concatenation operator, and the asterisk (*) is the repetition
operator.
For example:
my_list = [ 'p', 'y', 't', 'h', 'o', 'n' ]
1. print(my_list[0]) #output : p
2. print(my_list[3]) #output : h
3. my_list[4.0] # Error ! Only integer can be used for indexing
nested_list = [ "boss", [ 2, 0, 1,9]]
1. print(nested_list[0][1]) #output : o
2. print(nested_list([1],[3]) #output : 9
Python allows negative indexing for its sequences. The index
of -1 refers to the last item, -2 to the second last item and so
on.
For example:
my_list = [ 'p', 'y', 't', 'h', 'o', 'n' ]
1. print(my_list[-1]) #output : n
2. print(my_list[-5]) #output : y
How to slice lists in Python?
We can access a range of items in a list by using the slicing
operator (colon).
For example:
my_list = [ 'p', 'y', 't', 'h', 'o', 'n', 1, 2, 3 ]
1. print(my_list[2:5]) #output : t h o #elements from 3rd to 5th
2. print(my_list[ : -5]) #output : p y t h #elements from
beginning to 4th
3. print(my_list[5 : ] #output : n 1 2 3 #elements from 6th to end
4. print(my_list[ : ] #output : p y t h o n 1 2 3 # elements from
beginning to end
How to change or add elements to a list?
List are mutable, meaning, their elements can be changed
change an item or a range of items.
For Example:
Even = [ 2, 4, 6, 8 ]
1. Even[0] = 1 #change the 1st item
2. print(Even) #output : [ 1, 4, 6, 8 ]
3. Even[1:4] = [ 3, 5, 7 ] # change 2nd to 4th items
4. print(Even) #output : [ 1, 3, 5, 7 ]
We can add one item to a list using
append()
method or add
several items using
extend()
method.For Example:
odd = [1, 3, 5]
odd.append(7)
print(odd) #Output: [1, 3, 5, 7]
odd.extend([9, 11, 13])
print(odd)
#Output: [1, 3, 5, 7, 9, 11, 13]
We can also use + operator to combine two lists. This is also called concatenation.
The * operator repeats a list for the given number of times.
For Example:
odd = [1, 3, 5]
print(odd + [9, 7, 5]) #Output: [1, 3, 5, 9, 7, 5]
print(["re"] * 3)
#Output: ["re", "re", "re"]
we can insert one item at a desired location by using the method
insert()
or insert multiple items by squeezing it into an empty slice of a list.For Example:
odd = [1, 9]
odd.insert(1,3)
print(odd) #Output: [1, 3, 9]
odd[2:2] = [5, 7]
print(odd)
#Output: [1, 3, 5, 7, 9]
How to delete or remove elements from a list?
We can delete one or more items from a list using the keyword
del
. It can even delete the list entirely.For example:
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
print(my_list) #Output : ['p', 'r', 'b', 'l', 'e', 'm']
# delete multiple items
del my_list[1:5]
print(my_list) #Output: ['p', 'm']
# delete entire list
del my_list
print(my_list)
#Error: List not defined
We can use
remove()
method to remove the given item or pop()
method to remove an item at the given index.
The
pop()
method removes and returns the last item if index is not provided. This helps us implement lists as stacks (first in, last out data structure).
We can also use the
clear()
method to empty a list.For Example:
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
print(my_list) #Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list.pop(1)) #Output: 'o'
print(my_list) #Output: ['r', 'b', 'l', 'e', 'm']
print(my_list.pop(-1)) #Output: 'm'
print(my_list) #Output: ['r', 'b', 'l', 'e']
my_list.clear()
print(my_list)
#Output: []
Finally, we can also delete items in a list by assigning an empty list to a slice of elements.
>>> my_list = ['p','r','o','b','l','e','m']
>>> my_list[2:3] = []
>>> my_list
['p', 'r', 'b', 'l', 'e', 'm']
>>> my_list[2:5] = []
>>> my_list
['p', 'r', 'm']
Python List Methods:
Methods that are available with list object in Python programming are tabulated below.
They are accessed as
list.method()
. Some of the methods have already been used above.Python List Methods |
---|
append() - Add an element to the end of the list |
extend() - Add all elements of a list to the another list |
insert() - Insert an item at the defined index |
remove() - Removes an item from the list |
pop() - Removes and returns an element at the given index |
clear() - Removes all items from the list |
index() - Returns the index of the first matched item |
count() - Returns the count of number of items passed as an argument |
sort() - Sort items in a list in ascending order |
reverse() - Reverse the order of items in the list |
copy() - Returns a shallow copy of the list |
For Example:
my_list = [3, 8, 1, 6, 0, 8, 4]
print(my_list.index(8)) #Output : 1
print(my_list.count(8)) #Output : 2
my_list.sort()
print(my_list) #Output : [0, 1, 3, 4, 6, 8, 8]
my_list.reverse()
print(my_list)
#Output : [8, 8, 6, 4, 3, 1, 0]
No comments:
Post a Comment