List in Python
Introduction
It is a collection of items and each item has its own index value. Index of first item is 0 and the last item is [Link] n
is number of items in a list.
Creating a list
Lists are enclosed in square brackets [ ] and each item is separated by a comma.
Initializing a list
Passing value in list while declaring list is initializing of a list
e.g.
list1 = [‘English', ‘Hindi', 1997, 2000]
list2 = [11, 22, 33, 44, 55 ]
list3 = ["a", "b", "c", "d"]
Blank list creation
A list can be created without element
List4=[ ]
Iterating/Traversing Through A List
List elements can be accessed using looping statement.
e.g.
list =[3,5,9]
for i in range(0, len(list)):
print(list[i])
Output
3
5
9
List Operations
1. Joining List
2. Repeating or Replicating Lists
3. Slicing the List
4. Making True Copy of a List
Joining List – Joining of two list + operator is used
list1=[2,5,6]
list2=[“school”, 45.23]
list1+list2
[2,5,6, “school”, 45.23]
Repeating or Replicating Lists- * operator to replicate a list specified number of times.
List1=[1,3,5]
List1*3
[1,3,5,1,3,5,1,3,5]
Slicing the List-
Slicing is the subpart of a list extracted out.
Seq=L[start:stop] Note – stop value-1
list=[10,12,14,20,22,24,30,32,34]
Seq=list[3:-3]
[20,22,24]
e.g.
list =['I','N','D','I','A']
print(list[0:3])
print(list[3:])
print(list[:])
Output ['I', 'N', 'D'] ['I', 'A'] ['I', 'N', 'D', 'I', 'A']
Making True Copy of a List- Make a copy of a list .
a=[1,2,3]
b=a
List functions and methods
1. Len()- Return the length of its arguments
Syntax- len(list)
Example –
list=[1,2,3,4,5,6,7,8,9]
Len(list)
9
2. List () – Returns a list created the passed argument which should be a sequence type(string, list, tuple etc)
Syntax- list[sequence]
Example –
A=list((34,51,100))
24,51,100
3. Index() – returns the index of first matched item
Syntax- [Link](item)
Example –1
L1=[13,18,11,16,18,14]
[Link](18)
1
Example-2
fruits = ['apple', 'banana', 'cherry']
x = [Link]("cherry")
print(x)
2
4. Append()- Add an el;elemt to the end of the list
Syntax- [Link](item)
Example –
fruits = ["apple", "banana", "cherry"]
[Link]("orange")
print(fruits)
['apple', 'banana', 'cherry', 'orange']
5. Extend()- The extend() is also used for adding multiple elements (given in the form of list) to a list.
[Link](list)
Example –
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
[Link](cars)
['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']
6. Insert()- Adds an element at the specified position
Syntax- [Link](index, item)
Example –
fruits = ['apple', 'banana', 'cherry']
[Link](1, "orange")
['apple', 'orange', 'banana', 'cherry']
7. Remove()-Removes the first item with the specified value
Syntax- list. Remove(value)
Example –
fruits = ['apple', 'banana', 'cherry']
[Link]("banana")
['apple', 'cherry']
8. Clear()-Removes all the elements from the list
Example –
fruits = ['apple', 'banana', 'cherry', 'orange']
[Link]()
[]
9. Count()-Returns the number of elements with the specified value
Syntax- [Link](item)
Example –
fruits = ['apple', 'banana', 'cherry']
x = [Link]("cherry")
1
10. Revers()-Reverses the order of the list
Syntax- [Link]()
fruits = ['apple', 'banana', 'cherry']
[Link]()
['cherry', 'banana', 'apple']
11. Sort()- Sorts the list
Syntax- [Link]()
cars = ['Ford', 'BMW', 'Volvo']
[Link]()
['BMW', 'Ford', 'Volvo']
12. Min()- returns min value, Max()- return max value,Sum()- returns the sum values
Syntax- min(list), max(list), sum(list)
Example-
val=[17,24,15,30]
min(val) -15
max(val)- 30
sum(val)-86
Syntax- min (list)