List in Python
Lists are similar to arrays in C. However; the list can contain
data of different types. The items stored in the list are separated
with a comma (,) and enclosed within square brackets [].
We can use slice [:] operators, to access the data of the list. The concatenation operator (+) and repetition operator (*) works with
the list in the same way as they were working with the strings.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
print (list)
print (list[0])
print (list[2:4])
print (list[2:])
print (list * 2)
print (list + list)
Output:
['abc', 123, 'hi', 10.12, 10]
abc
['hi', 10.12]
['hi', 10.12, 10]
['abc', 123, 'hi', 10.12, 10, 'abc', 123, 'hi', 10.12, 10]
['abc', 123, 'hi', 10.12, 10, 'abc', 123, 'hi', 10.12, 10]
Access Python List
Python allows us to access values from the list in various ways.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
print (list[0])
print (list[2:4])
print (list[2:])
print (list[-3:-1])
Output:
abc
['hi', 10.12]
['hi', 10.12, 10]
['hi', 10.12]
Adding Python Lists
In Python, lists can be added by using the concatenation
operator(+) to join two lists.
Example:
list = [ 'abc', 123]
list1 = [ 'hi', 10.12 , 10 ]
print (list+list1)
Output:
['abc', 123, 'hi', 10.12, 10]
Replicating lists
Replicating means repeating, It can be performed by using '*'
operator by a specific number of times.
Example:
list = [ 'abc', 123]
print (list*2)
Output:
List Slicing
A subpart of a list can be retrieved on the basis of an index. This
subpart is known as list slice. This feature allows us to get sub-list
of specified start and end index.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
print (list[0])
print (list[2:4])
print (list[2:])
Output:
abc
['hi', 10.12]
['hi', 10.12, 10]
Updating List
To update or change the value of a particular index of a list,
assign the value to that particular index of the List.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
print (list)
list[2] = 'how'
print (list)
Output:
['abc', 123, 'hi', 10.12, 10]
['abc', 123, 'how', 10.12, 10]
Appending Python
List
Python provides, append() method which is used to append i.e.,
add an element at the end of the existing elements.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
list.append('how')
print (list)
Output:
['abc', 123, 'hi', 10.12, 10, 'how']
Deleting Elements
In Python, a del statement can be used to delete an element from
the list. It can also be used to delete all items from startIndex to
endIndex.
Example:
list = [ 'abc', 123 , 'hi', 10.12 , 10 ]
del list[2:4]
print (list)
output:
Following are the common list functions.
min() method
This method is used to get min value from the list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (min(list))
Output:
max() method
This method is used to get the max value from the list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (max(list))
Output:
len() method
This method is used to get the length of the list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (len(list))
Output:
Following are the built-in methods of List
index(object) Method
It returns the index value of the object.
Example:
list = [ 1, 123 , 102, 10.12 , 10]
print (list.index(102))
Output:
count(object) Method
It returns the number of times an object is repeated in the list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (list.count(102))
list1 = [ 1, 123 , 102, 102 , 10 ]
print (list1.count(102))
Output:
pop()/pop(index) Method
It returns the last object or the specified indexed object. It
removes the popped object.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (list.pop())
print (list.pop(2))
print (list)
Output:
insert(index,object) Method
It inserts an object at the given index.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
print (list.insert(3,20))
print (list)
Output:
[1, 123, 102, 20, 10.12, 10]
extend(sequence) Method
It adds the sequence to the existing list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
list1 = [ 3 , 4 ]
print (list.append(list1))
print (list)
Output:
[1, 123, 102, 10.12, 10, [3, 4]]
remove(object) Method
It removes the object from the given list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
list.remove(123)
print (list)
Output:
reverse()Method
It reverses the position of all the elements of a list.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
list.reverse()
print (list)
Output:
sort() Method
It is used to sort the elements of the List.
Example:
list = [ 1, 123 , 102, 10.12 , 10 ]
list.sort()
print (list)
Output: