0% found this document useful (0 votes)
25 views9 pages

9) Python Review List in Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

9) Python Review List in Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LIST

 A list is a standard data type of Python that can store a sequence of values of any data
type in square brackets[ ]. List is mutable i.e. its value can be changed or modified.
Examples:
[ ] – empty list i.e. list with no element
[2,3,4,5] – list of integer
[1,2.5,3.2,8,9] – list of numbers
[‘a’,’b’,’c’] – list of characters
[2,’a’,3.2,8,9,’b’] – list of mixed values

 Creating Lists :- We have the following ways to create list:


1. By specifying values in square brackets [ ] :-
L = [2,5,7,5,9]
P = [‘h’,’ e’, ’l’, ’l’, ’o’]

2. Using list() method :- It create a individual element of list from the individual
element of the specified sequence as givren below.
Syntax : L = list(sequence)
Here sequence can be string, tuple or list
Examples:-
>>>L = list(“bhel”) >>>T=(3,2,6,4) >>>L1=[6,3,2,8,7]
>>>L >>>L = list(T) >>>L2 = list(L1)
[‘b’, ’h’, ’e’, ’l’] >>>L >>>L2
[3,2,6,4] [6,3,2,8,7]

3. Using input() method :- We can also enter list elements during the execution of
program as follows:
Syntax : L = list(input(“Enter list elements : ”))

As we know that input() take the value from the user in form of string So the above
syntax will create a list of character. Because list() will take the argument as string.
Example:

L = list(input(“Enter elements of list”)) Output:


print(L) Enter elements of list: 3425
[‘3’, ‘4’, ‘2’, ‘5’]
4. Using eval() method :- The eval() identifies the type of the value entered by user and
assign the entered value in the same format to the variable i.e.
Syntax : a = eval(input(“Enter eny value : ”))

a = eval(input(“Enter any value”)) a = eval(input(“Enter any value”))


print(a, type(a)) print(a, type(a))

Output: Output:
Enter any value: 25 Enter any value: 3.5
25 <class : ‘int’> 3.5 <class : ‘float’>

a = eval(input(“Enter any value”)) a = eval(input(“Enter any value”))


print(a, type(a)) print(a, type(a))

Output: Output:
Enter any value: [4,2,6,7,8] Enter any value: (2,3,4,5)
[4,2,6,7,8] <class : ‘list’> (2,3,4,5) <class : ‘tuple’>
 Indexing, Accessing and Traversing list elements:- Individual element of list is
accessed by the help of its index. There are two types of indexing:
Forward Indexing
Backword Indexing
Example:- L = [4,7,9,8,20,15,12]

Forward indexing
0 1 2 3 4 5 6
4 7 9 8 20 15 12
-7 -6 -5 -4 -3 -2 -1
Backword indexing
Accessing list elements: Traversing a list:
Output:
L[1] = 7 L = [4,7,9,3] 4
L[5] = 15 for i in L: 7
L[-3] = 20 print(i) 9
3
L[-6] = 7
LIST OPERATIONS
 Joining Lists/Concatenation Operator (+) :- This operator creates a new list by joining
two lists.
example:
>>>L1 = [2,4,6,8]
>>>L2 = [1,3,5]
>>>L3 = L1+L2
>>>L3
[2,4,6,8,1,3,5]
In case of concatenation both operands must be list.

 Replicating lists/Replication Operator (*) :- Like string we can replicate a list specified
number of times.
example:
>>>L = [1, 2, 3, 4]
>>>L*2
[1, 2, 3, 4, 1, 2, 3, 4]
LIST SLICING
 Slicing of list is similar to string slicing. We can use indexes of list elemets to create
list slice and slicing of list always return a list.
Syntax:
L = List_name[start:end:step]
• start - starting index from where slicing will starts
• end - Position till which slicing will takes place. Slicing stops at end-1.
• step – It determines the increment between each index for slicing.
Examples:- 0 1 2 3 4 5 6
L = [5,12,15,30,10,25,8]
5 12 15 30 10 25 8
-7 -6 -5 -4 -3 -2 -1
>>>L[2 : 6] >>>L[0 : 6 : 2]
[15, 30, 10, 25] [5, 15, 10]
>>>L[-6 : -2] >>>L[1 : 15 : 3]
[12, 15, 30, 10] [12, 10]
>>>L[3 : 10] >>>L[: : 2]
[30, 10, 25, 8] [5, 15, 10, 8]
>>>L[8 : 12] >>>L[: : -1]
[] [8, 25, 10, 30, 15, 12, 5]
LIST FUNCTIONS
Syntax: <ListObject>.<function name>()
index() This function return the index of first matched item from the list.
>>>L=[7,3,5,9,2,4,20,5]
>>>L.index(5)
2
append() This function is used to add a element at the end of the list. The element
may be of type int, string or list. append() function does not return the
new list.
>>>L=[7,3,5,9,2,4]
>>>L.append(50)
>>>L
[7,3,5,9,2,4,50]
extend() This function is used to add multiple elements at the end of the string. It
takes the multiple elements in form of list. This function also does not
return any list.
>>>L1=[‘d’, ’p’, ’s’]
>>>L2=[‘x’, ‘y’]
>>>L1.extend(L2)
>>>L1
[‘d’, ’p’, ’s’, ‘x’, ‘y’]
LIST FUNCTIONS
insert() This function is used to add element somewhere in between th elist of at
the specified position of list. It takes two arguments <position> and
<item>. Syntax: List.insert(pos,item)
>>>L=[7,3,4,9,2] >>>L=[5,4,6,8]
>>>L.insert(3,5) >>>L.insert(len(L),12)
>>>L >>>L
[7,3,4,5,9,2] [5,4,6,8,12]
pop() This function is used to remove a element from the specified index of the
list. It return the element removed from the list. If no index is specified,
pop() removes the last element from the list.
>>>L=[7,3,5,9,2,4] >>>L=[7,3,5,9,2,4]
>>>L.pop(2) >>>L.pop()
>>>L >>>L
[7,3,9,2,4] [7,3,5,9,2]
remove() This function removes the first occurrence of the specified element from
the list.
>>>L=[‘d’, ‘p’, ‘s’, ‘r’]
>>>L.remove(‘s’)
>>>L
[‘d’, ’p’, ’r’]
LIST FUNCTIONS
clear() This function removes all the elements from the list and list become empty list.
>>>L=[7,3,5,9,2,4,20,5]
>>>L.clear()
>>>L
[]
count() This function return the number of occerance of the specified item in the list. If the
specified item is not present then this function return zero.
>>>L=[5,3,5,9,2,4] >>>L=[5,3,5,9,2,4]
>>>L.count(5) >>>L.count(7)
2 0
reverse() This function reverses the items of the list.
>>>L=[5,4,3,2]
>>>L.reverse()
>>>L
[2,3,4,5]
sort() This function sort the items of list , by default in increasing order.
In increasing order In becreasing order
>>>L=[5,3,7,9,2,4] >>>L=[5,3,5,9,2,4]
>>>L.sort() >>>L.sort(reverse=True)
>>>L >>>L
[2,3,4,5,7,9] [9,7,5,4,3,2]

You might also like