Python Lists

Python list is a sequential built-in data type. Each element of a sequence is assigned a number – its position or index. The first index is zero and so on.

Python has six built-in data types but the Python lists and Python tuples are the most common, In this tutorial, we will explore Python List in detail.  If you are interested in Python Tuples then you can check out our Tutorial on Python Tuples

Python Lists indexes

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Python Lists

The list is one of the most useful data types available in Python which can be written as a list of comma-separated values (elements) between square brackets. The most important thing about the list is – its item can be of any data type.

Creating a list is as simple as putting different values inside a comma-separated values between square brackets. some examples of lists are as follows

list1 = ['python' ,'programming','is', 'fun']
list2  = [10,20,30,40,50,60]
list3=["a","b","c","d","e"]
list4 = [10,'rakesh',34.56,"a","atul"]

Similar to Python strings, the list also has indexes that start from o onwards. List elements can be accessed using these indexes, slicing, concatenation, repetition, membership can be done on lists too.

Accessing List elements

To access list elements, use the index in the square bracket, and for slicing define the starting index and end index along with step value inside the square bracket. few examples

list1 = ['python' ,'programming','is', 'fun']
list2  = [10,20,30,40,50,60,70,80]

print(list1[2]     
#result : 'is'
print(list1[1]
#result # 'programming'

#slicing example 

print(list2[0:4]
#result  : 10,20,30,40 

Updating List elements

Since, List is a mutable data type ie its elements can be change during the runtime. You can update single or multiple values of the elements of the list giving the slice on the left hand side of the assignment operator.

Example

list1 = ['python' ,'programming','is', 'fun']
list2  = [10,20,30,40,50,60,70,80]

list1[3] ='very rewarding'
list2[0:3]=[100,200,300]

print(list1)
#result : ['python' ,'programming','is', 'very rewarding']

print(list2)

#result  : [100,200,300,40,50,60,70,80

Deleting List Elements

To remove a list element, you can use del statement or pop(), popitem() method.
Example

list1  = [10,20,30,40,50,60,70,80]
list1.pop()
# The above code always remove the last element of the list 

list1.pop(2)
# The above code removes the element present at index -2 in the list

list1.popitem(30)
#The above code removes element 30 from the list. Use the popitem() method when you know the element value. 

del list1[0]
#The above del statement removes the element from the list index -0

Concatenation and replication on list

Lists can be concatenated and replicated like the string datatype of Python. But they can not perform subtraction and division operation.
Example

>>> list1 = ['python' ,'programming','is', 'fun']
>>> list2  = [10,20,30,40,50,60,70,80]
>>> list3= list1+list2
>>> list3
['python', 'programming', 'is', 'fun', 10, 20, 30, 40, 50, 60, 70, 80]


>>> list1*2
['python', 'programming', 'is', 'fun', 'python', 'programming', 'is', 'fun']

Membership operation on Python List

Python Lists also support membership operation using in operator. Sample code is

>>> list1
['python', 'programming', 'is', 'fun']
>>> if 'is' in list1:
	print(' found ')

 found 

>>> if 'are' not in list1:
	print(' Are is not in the list')

	
 Are is not in the list

Iteration in Python List

Individual elements of a Python List can be accessed through python For loop. as for loop run through iterative values. Example

>>> list1
['python', 'programming', 'is', 'fun']
>>> for x in list1:
	print(x)

#output
	
python
programming
is
fun

Built-in Python List Functions and Methods

Sample List, based on that we will discuss built-in functions and methods and  Python Data Structure of lists.

list1 = [10,20,56,34,56,67,34,23,67,98,547]
FunctionsDescription
len ( )len ( ) function return total number of elements available in a list

print(len(list1))

# result  : 11

max( )max( ) function return the largest element present in the list

print(max(list1))

# result : 547

min( )min( ) function return the lowest element present in the list

print(min(list1))

#result : 10

sum( )sum( ) function return the sum of list elements

print(sum(list1))

#result : 1012

reverse( )reverse( ) method reverse all the elements of the list

print(list1.reverse)

# result : [547, 98, 67, 23, 34, 67, 56, 34, 56, 20, 10]

append( )append( ) method adds object to the list

>>> list1.append(list2)
>>> list1
[547, 98, 67, 67, 56, 56, 34, 34, 23, 20, [10, 20, 30, 40, 50, 60, 70, 80]]

extend( )Extend( ) method append the contents of sequence to list

>>> list1.extend(list2)
>>> list1
[547, 98, 67, 67, 56, 56, 34, 34, 23, 20, [10, 20, 30, 40, 50, 60, 70, 80], 10, 20, 30, 40, 50, 60, 70, 80]

sort( )sort( ) method is used to sort the list element in ascending/descending order

print(list1.sort())

[10, 20, 23, 34, 34, 56, 56, 67, 67, 98, 547]

print(list1.sort(reverse=True))

#  result : [547, 98, 67, 67, 56, 56, 34, 34, 23, 20, 10]

count( )count( ) method count the occurance of the argument.

print(list1.count(67))

#result : 2

index( )Index( ) method find out the first  index of the supplied value

print(list1.index(67))

#result : 2

insert( )insert( ) method is used to add a new element in the list at specified location

>>> list1.insert(2,100)
>>> list1
[547, 98, 100, 67, 67, 56, 56, 34, 34, 23, 20, 10]

pop( )pop method is used to remove an element from the list. When no index is passed as an argument it removes the last element.

>>> list1.pop()
10
>>> list1.pop(2)
100

list( )List( ) function is used to convert tuple into list
If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.