List
List
Python lists are mutable type it means we can modify its element after it created.
The items in the list are separated with the comma (,) and enclosed with the square
brackets [].
Ex:
l1 = ["John", 102, "USA"]
l2 = [1, 2, 3, 4, 5, 6]
print(l1)
print(l2)
print(type(l1))
print(type(l2))
O/P:['John', 102, 'USA']
[1, 2, 3, 4, 5, 6]
<class 'list'>
<class 'list'>
O/P: True
False
You access the list items by referring to the index number:
O/P: XYZ
Negative indexing means beginning from the end, -1 refers to the last
item, -2 refer to the second last item etc.
O/P: PQR
You can specify a range of indexes by specifying where to start and
where to end the range.
When specifying a range, the return value will be a new list with the
specified items.
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
list = [1, 2, 3, 4, 5, 6]
print(list)
list[2] = 10
print(list)
list[1:3] = [89, 78]
print(list)
list[-1] = 25
print(list)
O/P: [1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
Loop through a List
for i in l1:
The for loop is used to iterate print(i, end = ’ ’)
Iteration
over the list elements. Output:
1234
l =[]
n = int(input("Enter the number of elements in the list:"))
for i in range (0, n):
l.append (input ("Enter the item:") )
print("printing the list items..")
for i in l:
print(i, end = " ")
O/P: Enter the number of elements in the list:4
Enter the item:11
Enter the item:22
Enter the item:abc
Enter the item:5.5
printing the list items..
11 22 abc 5.5
O/P: [0, 1, 2, 3, 4]
[0, 1, 3, 4]
If we write – list.remove(5)
It will give an error -list.remove(x): x not in list
The pop() method removes the specified index, (or the last item if index is not
specified):
O/P: []
Copy a List:
You cannot copy a list simply by typing list2 = list1, 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 copy, one way is to use the built-in List method copy().
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
for x in list2:
list1.append(x)
list1.extend(list2)
print(list1) # ['a', 'b', 'c', 1, 2, 3]
The list() Constructor
It is also possible to use the list() constructor to make a new list.
Method Description
append() Adds an element at the end of the list.
clear() Removes all the elements from the list.
copy() Returns a copy of the list.
count() Returns the number of elements with the specified value.
Add the elements of a list (or any iterable), to the end of the
extend()
current list.
index() Returns the index of the first element with the specified value.
insert() Adds an element at the specified position.
pop() Removes the element at the specified position.
remove() Removes the item with the specified value.
reverse() Reverses the order of the list.
sort() Sorts the list.
len(list) It is used to calculate the length of the list.
max(list) It returns the maximum element of the list.
min(list) It returns the minimum element of the list.
list(seq) It converts any sequence to the list
Ex:
list1 = [1,2,3,4,5,2,3,2]
print(list1)
list1.reverse()
print("List in reverse order : ",list1)
list1.sort()
print("List in sorted order : ",list1)
Ex:
Str = "ABCD"
list2 = list(str)
print (list2)
print (str)
list1 = [1,2,3,4,5,2,3,2]
list2=[]
for i in list1:
if i not in list2:
list2.append(i)
print(list2)
list1 = [1,2,3,4,5,2,3,2]
sum=0
for i in list1:
sum=sum+i
print(sum)
3. Write the program to display elements of list which occurs more than
once.
list1=[1,2,3,4,5,3,6,7,3,5,3,1]
list2=[]
for i in range(0,len(list1)):
for j in range(i+1,len(list1)):
if list1[i]==list1[j] and list1[i] not in list2:
list2.append(list1[i])
break
print(list2)
OR
list1=[1,2,3,4,5,3,6,7,3,5,3,1]
list2=[]
for i in list1:
if list1.count(i)>1 and i not in list2:
list2.append(i)
print(list2)