4.list in python
4.list in python
Website: https://pythonlife.in/
Python List
A list in Python is used to store the sequence of various types of data.
Python lists are mutable type its mean we can modify its element after it
created.
However, Python consists of six data-types that are capable to store the
sequences, but the most common and reliable type is the list.
A list can be defined as a collection of values or items of different types.
The items in the list are separated with the comma (,) and enclosed with
the square brackets []
in
A list can be define as below
If we try to print the type of L1, L2, and L3 using type() function then it will
come out to be a list.
th
print(type(L1))
Py
print(type(L2))
Output:
<class ‘list’>
<class ‘list’>
2
Website: https://pythonlife.in/
Characteristics of Lists :
The list has the following characteristics:
•The lists are ordered.
•The element of the list can access by index.
•The lists are the mutable type.
•The lists are mutable types.
•A list can store the number of various elements .
in
The indexing is processed in the same way as it happens with the strings.
e.
The elements of the list can be accessed by using the slice operator [].
lif
The index starts from 0 and goes to length - 1. The first element of the list
on
is stored at the 0th index, the second element of the list is stored at the 1st
index,and soon.
th
Py
3
Website: https://pythonlife.in/
We can get the sub-list of the list using the following syntax.
list_varible(start:stop:step)
•The start denotes the starting index position of the list.
•The stop denotes the last index position of the list.
•The step is used to skip the nth element within a start:stop
in
list = [1,2,3,4,5,6,7]
print(list[0]) e.
lif
print(list[1])
on
print(list[2])
print(list[3])
th
print(list[0:6])
# By default the index value is 0 so its starts from the 0th element and go
for index -1.
print(list[:])
print(list[2:5])
print(list[1:6:2])
4
Website: https://pythonlife.in/
Unlike other languages, Python provides the flexibility to use the negative
indexing also. The negative indices are counted from the right. The last
element (rightmost) of the list has the index -1; its adjacent left element is
present at the index -2 and so on until the left-most elements are
encountered.
in
e.
lif
Let's have a look at the following example where we will use negative
on
list = [1,2,3,4,5]
print(list[-1])
Py
print(list[-3:])
print(list[:-1])
print(list[-3:-1])
As we discussed above, we can get an element by using negative indexing.
In the above code, the first print statement returned the rightmost element
of the list. The second print statement returned the sub-list, and so on.
5
Website: https://pythonlife.in/
Python also provides append() and insert() methods, which can be used to add values to the
list.
Consider the following example to update the values inside the list
list = [1, 2, 3, 4, 5, 6]
print(list)
list[2] = 10
print(list)
in
# Adding multiple-element
list[-1] = 25
th
print(list)
Py
6
Website: https://pythonlife.in/
Iterating a List
A list can be iterated by using a for - in loop. A simple list containing four
strings, which can be iterated as follows.
list = ["John", "David", "James", "Jonathan"]
for i in list:
# The i variable will iterate over the elements of the List and contains
each element in each iteration.
print(i)
in
Adding elements to the list
e.
Python provides append() function which is used to add an element to the list. However,
lif
the append() function can only add value to the end of the list.
on
Consider the following example in which, we are taking the elements of the list from the
user and printing the list on the console.
th
l =[]
for i in range(0,n):
# The input is taken from the user and added to the list as the item
for i in l:
7
Website: https://pythonlife.in/
print(i, end = " ")
for i in list:
print(i,end=" ")
in
list.remove(2)
e.
print("\nprinting the list after the removal of first element...")
lif
for i in list:
on
print(i,end=" ")
th
append()
Used for appending and adding elements to List. It is used to add elements to the last position
of the List in Python.
Syntax:
list.append (element)
in
insert()
Inserts an element at the specified position.
Syntax:
e.
lif
list.insert(<position, element)
on
Note: Position mentioned should be within the range of List, as in this case between 0 and 4,
elsewise would throw IndexError.
th
List.insert(2,10087)
print(List)
Output:
Syntax
List1.extend(List2)
List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]
Other functions of List
sum()
Calculates the sum of all the elements of the List.
Syntax
sum(List)
in
List = [1, 2, 3, 4, 5]
print(sum(List))
Output:
e.
lif
15
on
The sum is calculated only for Numeric values, elsewise throws TypeError.
th
Py
See example:
List = ['gfg', 'abc', 3]
print(sum(List))
Output:
count()
Calculates total occurrence of a given element of List.
10
Website: https://pythonlife.in/
Syntax:
List.count(element)
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))
Output:
4
length
Calculates the total length of the List.
Syntax:
len(list_name)
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
in
print(len(List))
Output:
10
e.
lif
index()
Returns the index of the first occurrence. The start and End index are not necessary
on
parameters.
Syntax:
th
List.index(element[,start[,end]])
Py
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))
Output:
1
Another example:
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2))
Output:
4
min()
Calculates minimum of all the elements of List.
11
Website: https://pythonlife.in/
Syntax:
Deletion of List Elements
To Delete one or more elements, i.e. remove an element, many built-in functions can be used,
such as pop() & remove() and keywords such as del.
pop()
The index is not a necessary parameter, if not mentioned takes the last index.
Syntax:
list.pop([index])
in
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())
Output:
e.
lif
2.5
on
Output:
Py
2.3
del()
Element to be deleted is mentioned using list name and index.
Syntax:
del list.[index]
remove()
Element to be deleted is mentioned using list name and element.
Syntax
list.remove(element)
in
e.
lif
on
th
Py