0% found this document useful (0 votes)
20 views

Python List

Uploaded by

CS BCA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python List

Uploaded by

CS BCA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PYTHON LIST

In Python, the sequence of various data types is stored in a list. A list is a collection of
different kinds of values or items. Since Python lists are mutable, we can change their
elements after forming. The comma (,) and the square brackets [enclose the List's items]
serve as separators.
List Declaration
Code
list1 = [1, 2, "Python", "Program", 15.9]
list2 = ["Ani", "Ram", "Hema", " Priya "]
print(list1)
print(list2)
print(type(list1))
print(type(list2))
Output:
[1, 2, 'Python', 'Program', 15.9]
[' Ani ', ' Ram ', ' Hema ', 'Priya']
< class ' list ' >
< class ' list ' >

Characteristics of Lists
The characteristics of the List are as follows:
o The lists are in order.
o The list element can be accessed via the index.
o The mutable type of List is
o The rundowns are changeable sorts.
o The number of various elements can be stored in a list.

PYTHON LIST OPERATIONS


The concatenation (+) and repetition (*) operators work in the same way as they were
working with the strings. The different operations of list are
1. Repetition
2. Concatenation
3. Length
4. Iteration
5. Membership
Let's see how the list responds to various operators.
1. Repetition
The redundancy administrator empowers the rundown components to be rehashed on
different occasions.
Code
list1 = [12, 14, 16, 18, 20]
l = list1 * 2
print(l)
Output:
[12, 14, 16, 18, 20, 12, 14, 16, 18, 20]

2. Concatenation
It concatenates the list mentioned on either side of the operator.
Code

list1 = [12, 14, 16, 18, 20]


list2 = [9, 10, 32, 54, 86]
l = list1 + list2
print(l)
Output:
[12, 14, 16, 18, 20, 9, 10, 32, 54, 86]

3. Length
It is used to get the length of the list
Code
list1 = [12, 14, 16, 18, 20, 23, 27, 39, 40]
len(list1)
Output:
9

4. Iteration
The for loop is used to iterate over the list elements.
Code
list1 = [12, 14, 16, 39, 40]
for i in list1:
print(i)
Output:
12
14
16
39
40

5. Membership
It returns true if a particular item exists in a particular list otherwise false.
Code
list1 = [100, 200, 300, 400, 500]
print(600 in list1)
print(700 in list1)
print(1040 in list1)
print(300 in list1)
print(100 in list1)
print(500 in list1)
Output:
False
False
False
True
True
True

PYTHON LIST BUILT-IN FUNCTIONS


Python provides the following built-in functions, which can be used with the lists.
1. len()
2. max()
3. min()
1. len( )
It is used to calculate the length of the list.
Code
list1 = [12, 16, 18, 20, 39, 40]
len(list1)
Output:
6
2. Max( )
It returns the maximum element of the list
Code
list1 = [103, 675, 321, 782, 200]
print(max(list1))
Output:
782

3. Min( )
It returns the minimum element of the list
Code
list1 = [103, 675, 321, 782, 200]
print(min(list1))
Output:
103

LIST INDEXING AND SPLITTING


The indexing procedure is carried out similarly to string processing. The slice operator [] can
be used to get to the List's components.
The index ranges from 0 to length -1. The 0th index is where the List's first element is stored;
the 1st index is where the second element is stored, and so on.
We can get the sub-list of the list using the following syntax.
1. list_varible(start:stop:step)
o The beginning indicates the beginning record position of the rundown.
o The stop signifies the last record position of the rundown.
o Within a start, the step is used to skip the nth element: stop.
The start parameter is the initial index, the step is the ending index, and the value of the end
parameter is the number of elements that are "stepped" through. The default value for the step
is one without a specific value. Inside the resultant Sub List, the same with record start would
be available, yet the one with the file finish will not. The first element in a list appears to
have an index of zero.
Consider the following example:
Code
1. list = [1,2,3,4,5,6,7]
2. print(list[0])
3. print(list[1])
4. print(list[2])
5. print(list[3])
6. # Slicing the elements
7. print(list[0:6])
8. # By default, the index value is 0 so its starts from the 0th element and go for index -1.
9. print(list[:])
10. print(list[2:5])
11. print(list[1:6:2])
Output:
1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]
In contrast to other programming languages, Python lets you use negative indexing as well.
The negative indices are counted from the right. The index -1 represents the final element on
the List's right side, followed by the index -2 for the next member on the left, and so on, until
the last element on the left is reached.

Let's have a look at the following example where we will use negative indexing to access the
elements of the list.
Code
1. # negative indexing example
2. list = [1,2,3,4,5]
3. print(list[-1])
4. print(list[-3:])
5. print(list[:-1])
6. print(list[-3:-1])
Output:
5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]
Negative indexing allows us to obtain an element, as previously mentioned. The rightmost
item in the List was returned by the first print statement in the code above. The second print
statement returned the sub-list, and so on.
Updating List Values
Due to their mutability and the slice and assignment operator's ability to update their values,
lists are Python's most adaptable data structure. Python's append() and insert() methods can
also add values to a list.
Consider the following example to update the values inside the List.
Code
1. # updating list values
2. list = [1, 2, 3, 4, 5, 6]
3. print(list)
4. # It will assign value to the value to the second index
5. list[2] = 10
6. print(list)
7. # Adding multiple-element
8. list[1:3] = [89, 78]
9. print(list)
10. # It will add value at the end of the list
11. list[-1] = 25
12. print(list)
Output:
[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
The list elements can also be deleted by using the del keyword. Python also provides us
the remove() method if we do not know which element is to be deleted from the list.
Consider the following example to delete the list elements.
Code
1. list = [1, 2, 3, 4, 5, 6]
2. print(list)
3. # It will assign value to the value to second index
4. list[2] = 10
5. print(list)
6. # Adding multiple element
7. list[1:3] = [89, 78]
8. print(list)
9. # It will add value at the end of the list
10. list[-1] = 25
11. print(list)
Output:
[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

You might also like