0% found this document useful (0 votes)
17 views

Notes Class 09 Python List Tuple

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Notes Class 09 Python List Tuple

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LIST &TUPLE iN PYTHON

Lists
The lists and tuples are Python’s compound data types. They are basically the same
types with one difference. List can be changed or modified i.e. mutable but tuples
cannot be changed or modified i.e. immutable.
A list in Python represents a list of comma separated values of any datatypes between
square brackets.

List items are ordered, changeable, and allow duplicate values.


List items are indexed, the first item has index [0], the second item has index [1] etc.
When we say that lists are ordered, it means that the items have a defined order, and
that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

Note: There are some list methods that will change the order, but in general: the
order of the items will not change.
Since lists are indexed, lists can have items with the same value. Lists allow duplicate
values.
Lists
To assign a list to a variable.
list1=[1,2,3,4,5]
list2=[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
list3=[‘DPS’, 12, ‘Bopal’]
List4=[] #empty list
list5=list() #creating an empty list using list function
list6=[3,4,[5,6],7,8] #nested list
print(type(list1)) #will display <class list>
print(list1[1]) #will print 2
print(list2[3]) #will print ‘o’
print(list2[-2]) #will print ‘o’
print(list6[-3])) #will display [5,6]
Lists
list7=list(‘DPS’)
print(list7) #will print [‘D’, ‘P’, ‘S’]
t1=(‘w’, ‘o’, ‘r’, ‘k’) # creates tuple t1
print(list(t1)) #will print [‘w’, ‘o’, ‘r’, ‘k’]

To get list from user


list7=list(input(“Enter a list:”))
Enter a list: 23456
print(list7) #will print [‘2’, ‘3’, ‘4’, ‘5’]
Refer list6
print(list6[1:3]) #will print [2,3]
print(list6[:3]) #will print [3,4,[5,6]]
print(list6[3:]) #will print [7,8]
print(list6[3:3]) []
print(list6[-4:-1]) [4,[5,6],7]
Lists
print(3 in list6) # will print True
print(list6[-4:-1]) #will print [4,[5,6],7]
list6[-3]=[5,6,7] # lists are mutable
print(list6) #will print [3,4,[5,6,7],7,8]
list6[0:1]=[1,2]
print(list6) #will print [1, 2, 4, [5, 6], 7, 8]
list6[1:3]=[10]
print(list6) #will print [3,10,7,8]
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the following)
insert(): To insert a new list item, without replacing any of the existing values
at the specified index.
print(list6.insert(2,6)) # will print [3,4,2,[5,6],7,8]

Append(): To add an item to the end of the list.


print(list6.append(10)) #will print[3,4,[5,6],7,8,10]

extend(): To append more than one elements or append from another list to the
current list, use the extend() method.
print(list6.extend(11,12)) #will print[3,4,[5,6],7,8,11,12]
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the following)
pop(): Removes the element at the specified position Note:
print(list6.pop(1)) # will print [3,[5,6],7,8] In python del is a keyword and
print(list6.pop()) # will print [3,[5,6],7] remove(), pop() are in-built
methods. The purpose of these
remove(): Removes the first item with the specified value three are same but the behavior is
print(list6.remove(7)) # will print [3,[5,6],8] different remove() method delete
values or object from the list using
value and del and pop() deletes
clear(): Removes all the elements from the list values or object from the list using
print(list6.clear()) # will print [] an index.

del: Python del function is used to delete an item from a list at a user-specified index. The
index position of the List del function starts from 0 and ends at n-1
del list6[0] #will print[4,[5,6],7,8]
del list6 # will delete the entire list
print(list 6) # will show error ‘list6 not defined’
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the following)

copy(): Returns a copy of the list


l6=list6.copy()
print(l6) # will print [3,4,[5,6],7,8]
count(): Returns the number of elements with the specified value.
print(list6.count(4) # will display 1
index(): Returns the index of the first element with the specified value
print(list6.index(3) # will display 0
reverse(): Reverses the order of the list
print(list6.reverse() # will display [8,7,[5,6],4,3]
Tuples
Tuples are used to store multiple items in a single variable.
A tuple is a collection which is ordered and unchangeable and allow duplicate
values.. So tuples are immutable.
Tuples are written with round brackets. Tuple items are indexed, the first item has
index [0], the second item has index [1] etc.
t=(1,2,3,4,5)
When we say that tuples are ordered, it means that the items have a defined order,
and that order will not change.
Tuples are unchangeable, meaning that we cannot change, add or remove items after
the tuple has been created.
Since tuples are indexed, they can have items with the same value.
print(len(t)) # will print 5
print(type(t)) # will print <class typle>
Tuples
While creating tuple using one element, always use comma after the element.
Otherwise Python will treat it as an integer/string based on a value which you
will put in.
tup1=(10)
print(tup1)) # will print <class int>
tup2=(10,)
print(type(tup2)) # will print <class tuple>
tup3=(1,2,3,4,5)
a,b,c,d,e=tup3
print(tup3) # will print (1,2,3,4,5)
print(a,b,c,d,e) # will print 1 2 3 4 5
del tup3 # will delete the tuple
DIFFERENCES BETWEEN LIST AND TUPLE IN PYTHON
Sno LIST TUPLE
1 Lists are mutable Tuples are immutable

The implication of iterations is The implication of iterations is


2
Time-consuming comparatively Faster

Tuple consumes less memory as


4 Lists consume more memory
compared to the list

Lists have several built-in Tuple does not have many built-in
5
methods methods.

Unexpected changes and errors


6 In a tuple, it is hard to take place.
are more likely to occur
Practice Program in List
1. WAP to find the first and last item of a list.
2. WAP to display all the content of list in a single line separated by a given character
3. WAP create a list of 5 names by accepting it from the user and display the list
4. WAP to find the largest and smallest number in a numeric list. Get content of the list from user.
5. WAP to insert a number in a given index. Get the elements of the list from the user.
6. WAP to add an element at the end of the list using insert function as well as append function. Get the
elements of the list from the user.
7. WAP to interchange first and last element of a list
8. WAP to reverse the list
9. WAP to whether the given element is present in a list or not.
10. WAP to find the sum of all numbers in a list. Get numeric list from the user.
11. WAP to get 2 different list and merge these two lists together to make one list.
12. WAP to count the no. of elements in a list without using functions
13. WAP to create n terms of Fibonacci Series (0,1,1,2,3,5,8,13,…..)
14. WAP to remove a specific item from a list
15. WAP to split the given list in such a way that all odd index position in one list & even index position
elements in another list

You might also like