2. Creating List
• Lists in Python can be created by just placing the sequence inside the
square brackets[ ].
• Creating list:
• Initial black list
• List with the use of String
• Use of Multiple Values
• Multi-dimesional list
4/3/2020LIST IN PYTHON 2
3. List Index
â–Ş We can use the index operator [] to access an item in a list. Index starts from 0. So, a
list having 5 elements will have index from 0 to 4.
â–Ş Nested list are accessed using nested indexing.
â–Ş Python allows negative indexing for its sequences. The index of -1 refers to the last
item, -2 to the second last item and so on.
4/3/2020LIST IN PYTHON 3
4. List Method:Append
• append( ) :
➢ It is built-in function
➢ Only one element at a time can be added to the list.
➢ For addition of multiple elements with append ( ) method loops are used.
➢ Tuples,Sets,Lists can also be added to existing list with the use of append( ) method.
4/3/2020LIST IN PYTHON 4
5. List Method :Insert
• insert( ) :
➢ It is built-in function
➢ The insert() method inserts an element to the list at a given index.
➢ The syntax of insert() method is:
list.insert(index,element)
➢ The insert() function takes two parameters:
• index - position where an element needs to be inserted
• element - this is the element to be inserted in the list
• This method does not return anything.Return none( ).
4/3/2020LIST IN PYTHON 5
6. List Method :Extend
• extend ( ) :
➢ It is built-in function
➢ The extend() extends the list by adding all items of a list (passed as an argument) to
the end.
➢ The syntax of insert() method is:
list1.extend(list2)
• If you need to add elements of other native datatypes (like tuple and set) to the list.
• E.g.list1.extend(list(tuple_type))
4/3/2020LIST IN PYTHON 6
7. Slicing List
• We can access a range of items in a list by using the slicing operator (colon).
my_list = ['c','o','r','o','n','a','2','0','2','0']
# elements 3rd to 5th
print(my_list[2:5])
# elements beginning to 4th
print(my_list[:-5])
# elements 6th to end
print(my_list[5:])
# elements beginning to end
print(my_list[:])
C O R O N A 2 0 2 0
0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Indexing
Negative
Indexing
4/3/2020LIST IN PYTHON 7
8. Add element from List
• List are mutable, meaning, their elements can be changed
unlike string or tuple.
• We can use assignment operator (=) to change an item or a range of items.
# mistake values
odd = [2, 4, 6, 8]
# change the 1st item
odd[0] = 1
# Output: [1, 4, 6, 8]
print(odd)
# change 2nd to 4th items
odd[1:4] = [3, 5, 7]
# Output: [1, 3, 5, 7]
print(odd)
4/3/2020LIST IN PYTHON 8
9. Delete element from List
• We can delete one or more items from a list using the keyword del. It can
even delete the list entirely.
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
print(my_list)
# delete multiple items
del my_list[1:5]
print(my_list)
# delete entire list
del my_list
print(my_list)
4/3/2020LIST IN PYTHON 9
10. Delete slice of items from List
• We can also delete items in a list by assigning an empty list to a slice of elements.
my_list = ['p','r','o','b','l','e','m’]
my_list[2:3] = []
print(my_list)
my_list[2:5] = []
print(my_list)
4/3/2020LIST IN PYTHON 10
11. Remove( ) item from List
• We can use remove() method to remove the given item
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
4/3/2020LIST IN PYTHON 11
12. Pop( ) item from List
• pop() method to remove an item at the given index.
• The pop() method removes and returns the last item if index is not provided.
• This helps us implement lists as stacks (first in, last out data structure).
print(my_list.pop( ))
print(my_list)
# Output: ['p','o','b','l','e']
my_list = ['p','r','o','b','l','e','m’]
print(my_list.pop(1))
print(my_list)
# Output: ['p','o','b','l','e','m’]
4/3/2020LIST IN PYTHON 12
13. Clear List
• We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m’]
my_list.clear()
#Output : [ ]
4/3/2020LIST IN PYTHON 13
14. + and * operator
• We can also use + operator to combine two lists. This is also called concatenation.
• The * operator repeats a list for the given number of times.
odd = [1, 3, 5]
# Output: [1, 3, 5, 9, 7, 5]
print(odd + [9, 7, 5])
#Output: ["re", "re", "re"]
print(["re"] * 3)
4/3/2020LIST IN PYTHON 14
15. Count ( )
• count() method returns count of how many times obj occurs in list.
• Syntax :
list_name.count(object)
•
my_list = [3, 8, 1, 6, 0, 8, 4]
# Output: 2
print(my_list.count(8))
4/3/2020LIST IN PYTHON 15
16. Index ( )
• Index( ) method returns the index of the first matched item.
• Syntax :
list_name.index(object)
my_list = [3, 8, 1, 6, 0, 8, 4]
# Output: 1
print(my_list.index(8))
4/3/2020LIST IN PYTHON 16
17. Index ( )
• Returns the index of first occurrence. Start and End index are not necessary parameters.
Syntax:
• list.index(element[,start[,end]])
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2))
# will check from index 2.
#Output
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2,6))
# will check from index 2 to 5.
#Output : 4
4/3/2020LIST IN PYTHON 17
18. sort( )
• The sort() method sorts the elements of a given list in a specific order - Ascending or
Descending.
• The syntax of sort() method is:
list.sort(key=..., reverse=...)
• Alternatively, you can also use Python's in-built function sorted() for the same purpose.
• sorted(list, key=..., reverse=...)
• Note: Simplest difference between sort() and sorted() is: sort() doesn't return any value
while, sorted() returns an iterable list.
4/3/2020LIST IN PYTHON 18
19. sort( )
• The sort function can be used to sort the list in both ascending and descending order.
• Syntax:
• Listname.sort( )
• This will srt the given list in ascending order.
• Syntax
• list_name.sort(reverse=True)
• This will sort the given list in descending order.
4/3/2020LIST IN PYTHON 19
20. sort( )
• To sorts according to user’s choice.
• It has two optional parameters:
reverse – If true, the list is sorted in descending order
key – function that serves as a key for the sort comparison
• It returns a sorted list according to the passed parameter.
4/3/2020LIST IN PYTHON 20
21. Reverse( )
• The reverse( ) method reverses the elements of a given list.
• Syntax:
listname.reverse( )
• The reverse ( ) function doesn’t take any argument.
• Reverse a list using slicing operator.
• E.g. Reverse_list=listname[::-1]
• To access individual elements of a list in reverse order,to use reversed( )
method.
4/3/2020LIST IN PYTHON 21
22. Copy
• The copy( ) method returns a shallow copy of the list.
• It doesn’t take any parameter.
• Syntax :
• New_list=listname.copy( )
• A list can be copied with = operator.
• Shallow copy of a list using slicing.
4/3/2020LIST IN PYTHON 22
23. SUM,LEN
• It returns the sums up the numbers in the list.
• Syntax:
• sum(iterable,start)
• Calculates total length of List.
• Syntax:
• len(list_name)
4/3/2020LIST IN PYTHON 23
24. MIN,MAX,CMP
• Calculates minimum of all the elements of List.
• Syntax:
• min(List)
• Calculates maximum of all the elements of List.
Syntax:
• max(List)
• Compares elements of two lists.
• Syntax:
• cmp(list1, list2)
4/3/2020LIST IN PYTHON 24
25. LIST
• list() takes sequence types and converts them to lists. This is used to
convert a given tuple into list.
• Syntax:
• list(seq)
4/3/2020LIST IN PYTHON 25
26. Summary
• List is a data type of container in Data Structures, which is used to store data
• Homogeneous (same)
• Hetrogeneous(different)
• List are ordered and have a definite count.
• List is mutable meaning you can change their content without changing their identity or
changeable(Item can be alter)
4/3/2020LIST IN PYTHON 26