100% found this document useful (1 vote)
188 views

Python List

The document summarizes common list methods and operations in Python. Some key points: - Lists can contain elements of different types and are defined using square brackets ([]). Elements are separated by commas. - Lists allow indexing and slicing of elements, with negative indexing counting from the right. - Common list methods include append(), copy(), insert(), pop(), remove(), count(), sort(), and clear() for modifying, accessing, counting, sorting and clearing list elements.

Uploaded by

Sakshi gautam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
188 views

Python List

The document summarizes common list methods and operations in Python. Some key points: - Lists can contain elements of different types and are defined using square brackets ([]). Elements are separated by commas. - Lists allow indexing and slicing of elements, with negative indexing counting from the right. - Common list methods include append(), copy(), insert(), pop(), remove(), count(), sort(), and clear() for modifying, accessing, counting, sorting and clearing list elements.

Uploaded by

Sakshi gautam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Python List

Methods and Operations


Python List

List in python is implemented to store the


sequence of various type of data.
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 [].
Example
L1 = [“bca", 102, “ajai"]  
L2 = [1, 2, 3, 4, 5, 6]  
L3 = [1, “hoshiar"]  
List indexing and splitting

The indexing are processed in the same


way as it happens with the strings. The
elements of the list can be accessed by
using the slice [:] operator : .
The index starts from 0 and goes to length -
1. The first element of the list is stored at
the 0th index, the second element of the
list is stored at the 1st index, and so on.
Consider the following example.
List cont …
Unlike other languages, python provides
us the flexibility to use the negative
indexing also.
 The negative indices are counted from
the right. The last element (right most)
of the list has the index -1, its adjacent
left element is present at the index -2
and so on until the left most element is
encountered
List cont …
Updating List values

Lists are the most versatile data structures


in python since they are immutable and
their values can be updated by using the
slice and assignment operator.
Python also provide us the append()
method which can be used to add values to
the string.
Consider the following example to update
the values inside the list.

List = [1, 2, 3, 4, 5, 6]   
print(List)   
List[2] = 10;   Output:
print(List)  
[1,
List[1:3] = [89, 78]   
2, 3, 4, 5, 6]
print(List)   [1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5,
6]
Python List copy() Method

Python copy() method copies the list and


returns the copied list. It does not take any
parameter and returns a list.
Signature
copy() 
Python list copy() Method  
# Creating a list  
evenlist = [6,8,2,4] # int list  
copylist = []  
# Calling Method  
copylist = evenlist.copy()  
# Displaying result  
print("Original list:",evenlist)  
print("Copy list:",copylist)  
Output:
Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
Python list copy() Method  

# Creating a list  
evenlist = [6,8,2,4] # int list  
copylist = []  
# Calling Method  
copylist = evenlist[:]  # Copy all the element
s  
# Displaying result  
print("Original list:",evenlist)  
print("Copy list:",copylist)  
Python List insert(i,x) Method

 Python insert() method inserts the element at


the specified index in the list. The first
argument is the index of the element before
which to insert the element.
 Signature
 insert(i, x)  
 Parameters
 i : index at which element would be inserted.
 x : element to be inserted.
Python List insert() Method
Example 1

example to insert an element at 3 index of the list.


# Creating a list  
list = ['1','2','3']  
Output:
for l in list:  # Iterating list  
    print(l)   123
list.insert(3,4)   After extending:
print("After extending:")   1234
for l in list:  # Iterating list  
    print(l)  
# Python list insert() Method  

# Creating a list  
list = ['1','2','3']  
for l in list:  # Iterating list  
    print(l)   Output:
       1 2 3 After inserting:
1 2 3 ['4', '5', '6']
list.insert(3,['4','5','6'])  
print("After inserting:")  
for l in list:  # Iterating list  
    print(l) 
Python List pop() Method

Python pop() element removes an element


present at specified index from the list. It
returns the popped element.
Signature
pop([i])  
Parameters
x : Element to be popped. It is optional.
# Python list pop() Method  

# Creating a list  
list = ['1','2','3']  
for l in list:  # Iterating list  
    print(l)  
list.pop(2)  
print("After poping:")  
for l in list:  # Iterating list  
    print(l)  
 Output:
 1 2 3 After poping: 1 2
Python List remove(x) Method

Python remove() method removes the


first item from the list which is equal to
the passed value. It throws an error if the
item is not present in the list. Signature
and examples are described below.
Signature
remove(x)  
Parameters
x : Element to be deleted.
List remove()

# Python list remove() Method  
# Creating a list  
list = ['1','2','3']  
for l in list:  # Iterating list  
    print(l)  
list.remove('2')  
print("After removing:")  
for l in list:  # Iterating list  
    print(l) 
output
1 2 3 After removing: 1 3
Python List remove() Method
If list contains duplicate elements, the
method will remove only first occurred
element. See the example below.
Python list remove() Method

# Creating a list
list = ['1','2','3','2']
for l in list: # Iterating list
print(l)
list.remove('2')
print("After removing:")
for l in list: # Iterating list
print(l)
Output:
1 2 3 2 After removing: 1 3 2
Python List count() Method

Python count() method returns the


number of times element appears in the
list. If the element is not present in the list,
it returns 0. The examples and syntax is
described below.
Signature
count(x)  
Parameters
x: element to be counted.
Python list count() Method  

# Creating a list  
apple = ['a','p','p','l','e']  
# Method calling  
count = apple.count('p')  
# Displaying result  
print("count of p :",count)  
Python List sort() Method

Python sort() method sorts the list


elements. It also sorts the items into
descending and ascending order.
It takes an optional parameter 'reverse'
which sorts the list into descending order.
 By default, list sorts the elements into
ascending order.
 Python list sort() Method  

# Creating a list  
apple = ['a', 'p', 'p', 'l', 'e'] # Char list  
even = [6,8,2,4] # int list  
print(apple)  
print(even)  
# Calling Method  
apple.sort()  
even.sort()  
# Displaying result  
print("\nAfter Sorting:\n",apple)  
print(even)  
List sort
# Creating a list  
even = [6,8,2,4] # int list  
# Calling Method  
#apple.sort()  
even.sort(reverse=True)   sort in reverse o
rder  
# Displaying result  
print(even) 
Python List clear() Method

Python clear() method removes all the


elements from the list. It clear the list
completely and returns nothing. It does not
require any parameter and returns no
exception if the list is already empty.
Signature
clear()  
Parameters
No parameter
# Python list clear() Method  

# Creating a list  
list = ['1','2','3']  
for l in list:  # Iterating list  
    print(l)  
list.clear()  
print("After clearing:")  
for l in list:  # Iterating list  
    print(l)  
Output:
1 2 3 After clearing:

You might also like