Python List
Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data type used in Python.
a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.). A list can even have another list as an item. These are called nested list.
All List elements are accessed by an index.
Python also supports computed lists, called “list comprehensions”
Syntax:
L = list() # empty list
L = list(sequence)
L = list(expression for variable in sequence)
Index
V |
I |
S |
I |
O |
N |
0 |
1 |
2 |
3 |
4 |
5 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Program on Lists
a=["prasad","balaram","gayathri","komali","vision","swamy"]
print(a)
print(a[0])
print(a[3])
print(a[5])
print(a[1])
program on lists with different types
a=[100,"prasad",2300]
print("Empno ",a[0],"Name: ",a[1],"Salary :",a[2])
program on lists with loops
a=[100,"prasad",2300]
for i in a:
print(i)
slice lists in
We can access a range of items in a list by using the slicing operator (colon).
program to print the slicelist
a=[4,3,2,3,55,8,7,6,9]
print(a[5:8]) # prints between 5th and 8th element
print(a[:-2])
print(a[5:])
print(a[:])
Negative 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.
Program on negative index
a=[4,3,2,3,55,8,7,6,9]
print(a[-2])
add elements to a 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.
Program to modify the elements
a=[4,3,2,3,55,8,7,6,9]
print(a)
a[1]=15
print(a)
a[2:4]=[55,66,77]
print(a)
append() : using this method we can add element to a list
extend() : using this method we can add several elements to a list
program on append and extend
a=[4,3,2,3,55,8,7,6,9]
print(a)
a.append(10)
print(a)
a.extend([11,12,13])
print(a)
+ Combine the List
* Repeats the list
List Operations
Python Expression |
Results |
Description |
len([1, 2, 3]) |
3 |
Length |
[1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Concatenation |
['Hi!'] * 4 |
['Hi!', 'Hi!', 'Hi!', 'Hi!'] |
Repetition |
3 in [1, 2, 3] |
True |
Membership |
for x in [1, 2, 3]: print x, |
1 2 3 |
Iteration |
Program on combine list
a=[4,3,2]
b=[10,11,12]
print(a,b)
c=a+b+[13,14,15]
print (c)
Program on Repeat the list
a=[4,3,2]
c=a*2
print(c)
d=a*5
print(d)
delete or remove elements from a list
We can delete one or more items from a list using the keyword del. It can even delete the list entirely.
remove() method to remove the given item or pop() method to remove an item at the given index.
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).
clear() method to empty a list
Program to delete the element from list
a=[4,3,2,6,7,1]
print(a)
del a[2] # deletes 2nd character
print(a)
program to delete the multiple elements
a=[4,3,2,6,7,1]
print(a)
del a[2:4]
print(a)
program on remove method
a=['v','i','s','i','o','n']
print(a)
a.remove('s')
print(a)
a.remove('i') # first occurence of i will remove
print(a)
program on pop and clear methods
a=['v','i','s','i','o','n']
print(a)
a.pop(2) # removes s
print(a)
a.clear()
print(a)
List Functions & Methods:
Function with Description |
cmp(list1, list2) |
len(list) |
max(list) |
min(list) |
list(seq) |
Python includes following list methods
Methods with Description |
list.append(obj) |
list.count(obj) |
list.extend(seq) |
list.index(obj) |
list.insert(index, obj) |
list.pop(obj=list[-1]) |
list.remove(obj) |
list.reverse() |
list.sort([func]) |
Program on list methods
a=['v','i','s','i','o','n']
print(a)
a.append('x')
print(a)
print(a.count('i'))
b=['p','r','a','s','d']
a.extend(b)
print(a)
print(a.index('s'))
a.insert(2,'d')
print(a)
print(a.pop())
print(a)
print(a.pop(3))
a.remove('d')
print(a)
a.reverse()
print(a)
a.sort()
print(a)
print(len(a))
print(max(a))
print(min(a))
program on nested lists
a=["prasd",[1,2,3,4],[7,8,9,6]]
print(a)
print(a[0][2])
print(a[1][1])
print(a[2][3])
List Comprehension
List comprehension is an elegant and concise way to create new list from an existing list in Python.
List comprehension consists of an expression followed by for statement inside square brackets.
Program on comprehension
p = [5 * x for x in range(10)]
print(p)
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743