Lists in Python
If you insert more items than you replace, the new items will be
inserted where you specified, and the remaining items will move
accordingly:
Example
Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)
List Functions and Methods
len() - To determine how many items a list has, use the len() function:
Example - Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
3
list() – Returns a list created from the passed argument, which should be a sequence type (string,
list, tuple etc.). If no argument is passed, it will create an empty list.
>>>list(“hello”)
[‘h’, ’e’, ’l’, ’l’, ‘o’ ] a=list(input(“enter list”)
>>>a=list() 567
Output – [‘5’, ‘6’, ‘7’]
[]
append() - To add an item to the end of the list.
Example:
L1 = ["apple", "banana", "cherry"]
[Link]("orange")
print(L1)
["apple", "banana", "cherry”, ”orange”]
extend() - To append elements from another list to the current list
Example
L1 = ["apple", "banana", "cherry"]
L2 = ["mango", "pineapple", "papaya"]
[Link](L2)
print(L1)
["apple", "banana", "cherry”, "mango", "pineapple", "papaya"]
insert() – inserts an item at a given position.
Syntax – [Link] (<ind>, <item>)
The first argument is the index of the element before which to insert,
[Link](0, x) #inserts at the front of the list, and
[Link](len(a), x) is equivalent to [Link](x).
pop() - Remove the item at the given position in the list, and return it. If no index is specified, [Link]() removes and returns
the last item in the list.
Syntax – [Link] (<index>) #index ix optional
a=[‘a’, ‘e’, ‘I’, ‘o’, ‘u’]
[Link]()
Print(a)
[‘a’, ‘e’, ‘I’, ‘o’]
[Link](1)
[‘a’, ‘I’, ‘o’]
del removes the specified element from the list, but does not return the deleted value.
L1 = [1, 5, 4, 70, 10, 90, 80]
>>> del L1 [4]
>>> print (L1)
[1, 5, 4, 70, 90, 80]
remove() - Remove the first item from the list whose value is equal to x.
It raises a ValueError if there is no such item.
Syntax - [Link](value)
#takes one essential argument and does not return anything
a=[‘a’, ‘e’, ‘I’, ‘o’, ‘u’]
[Link](‘e’)
print(a)
[‘a’, ‘I’, ‘o’, ‘u’]
clear() - Remove all items from the list and the list becomes empty list.
Equivalent to del a[:].
[Link]() #remove all items
print(a)
[]
count() - Return the number of times x appears in the list. If the given item is not in the
list, it returns zero.
L1=[10, 20, 30, 10]
[Link](10)
reverse() - Reverse the elements of the list in place.
L1=[3,4,5]
[Link]() #takes no argument , returns no list
print(L1)
[5,4,3]
sort() - Sort the items of the list, by default in ancreasing order. This is done in place (it does not create a
new list.
Syntax – [Link][<reverse =False/True>]
a=[5,2,3,4,1]
[Link]()
print(a)
[1,2,3,4,5]
sorted() – takes name of the list as an argument and returns a new sorted list with sorted elements.
val=[12,14,34,45]
sval=sorted(val)
print(sval)
[12,14,34,45]
rval=sorted(val,reverse=True)
print(rval)
[45,34,14,12]
min() – takes list sequence and return the minimum element.
>>>min(val)
12
max() - takes list sequence and return the maximum element.
>>>max(val)
45
sum() - takes list sequence and return the sum of elements.
>>>sum(val)
105
Copy a List
You cannot copy a list simply by typing
list2 = list1 list1 list2
because: list2 will only be a reference to list1, and changes made
in list1 will automatically also be made in list2.
There are ways to make a true copy, one way is to use the built-in List
method copy() and list():
Make a copy of a list with the copy() method: list1
list2 = copy(list1)
Make a copy of a list with the list() method: list2
list2 = list(list1)
L1=[2,3,5,7]
For I in L1: for I in range(4):
2,3,5,7 0,1,2,3
print(i) print(L1[i])
PYTHON LISTS
List
• A list in Python is used to store the sequence of various
types of data.
• It also allows duplicate members.
• A list can be defined as a collection of values or items
of same or different types,
• The items of a list are separated with comma(,) and
enclosed in square brackets[].
Write a program to make a list containing 10,’ten’,5,’five’,’six’
Change the 1 element with ‘nine’
Slice the list to print 3 to 5 elements
Check whether the word five is in the list or not.