Lists Tuples
Lists Tuples
1
07-05-2023
Lists
A list in Python is an ordered group of items or elements, and these list
elements don't have to be of the same type.
Python Lists are mutable objects that can change their values.
A list contains items separated by commas and enclosed within square
brackets.
List indexes like strings starting at 0 in the beginning of the list and
working their way from -1 at the end.
Similar to strings, Lists operations include slicing ([ ] and [:]) ,
concatenation (+), repetition (*), and membership (in).
This example shows how to access, update and delete list elements:
Lists
• L1=[1,2,4,5,’Pooja’,’iiitn’]
print(L1)
• Access
access the list items by referring to the index number:
L1[2]
Negative indexing means beginning from the end, -1 refers to the last
item, -2 refers to the second last item etc.
L1[-1] -Last item
2
07-05-2023
Range of indexes
• print(L1[2:5]
• the return value will be a new list with the specified items.
• print(L1[:4])
• print(L1[2:])
• print(L1[-4:-1])
Modifications
• To change the value of a specific item, refer to the index number:
L1[1]=‘Nagpur’
• Loop Through a List
for x in L1:
print(x)
• Check if Item Exists
if "apple" in L1:
print("Yes, 'apple' is in the fruits list")
• Length
print(len(L1))
3
07-05-2023
Adding items
• To add an item to the end of the list, use the append() method:
L1.append("orange")
• To add an item at the specified index, use the insert() method:
L1.insert(1, "orange")
Deleting items
• The remove() method removes the specified item:
L1.remove(“iiit")
• The pop() method removes the specified index, (or the last item if index is
not specified):
L1.pop()
• The del keyword removes the specified index:
del L1[0]
• The del keyword can also delete the list completely:
del L1
The clear() method empties the list:
L1.clear()
4
07-05-2023
Copy a List
• 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().
mylist = L1.copy()
• Another way to make a copy is to use the built-in method list().
mylist = list(L1)
5
07-05-2023
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
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Tuples
• A tuple is a collection which is ordered and unchangeable. In Python
tuples are written with round brackets.
tup=(“iiit",”nagpur",”keyboard")
print(thistuple)
Accessing an element
print(tup[1])
print(tup[-1])
print(tup[2:5])
print(tup[-4:-1])
6
07-05-2023
Change values
• Once a tuple is created, you cannot change its values. Tuples
are unchangeable, or immutable as it also is called.
• But there is a workaround. Convert the tuple into a list, change the list, and
convert the list back into a tuple.
• x = (“keyboard", “mouse", “laptop")
y = list(x)
y[1] = “desk"
x = tuple(y)
print(x)
• Once a tuple is created, one cannot add items to it. Tuples
are unchangeable.
7
07-05-2023
8
07-05-2023
Tuple methods
Method Description
index() Searches the tuple for a specified value and returns the position of where it was
found
Sets
• A set is a collection which is unordered and unindexed. In Python sets
are written with curly brackets.
aset = {“mango”, ”apple”, “banana”, “pineapple”}
print(aset)
Sets are unordered, not sure in which order the items will appear
9
07-05-2023
for x in aset:
print(x)
Once a set is created, its items cannot be changed but new items can
be added
10
07-05-2023
If the item to remove does not exist, remove() will raise an error.
The clear() method empties the set
aset.clear()
11
07-05-2023
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
12
07-05-2023
Dictionary
• A dictionary is a collection which is unordered, changeable and
indexed. In Python dictionaries are written with curly brackets, and
they have keys and values.
• Dict={‘Name’:’Pooja’,’Gender’:’Female’,’Organization’:’IIITN’}
• Dictionaries are used to store data values in key:value pairs.
• A dictionary is a collection which is ordered*, changeable and do not
allow duplicates.
• Dictionaries are written with curly brackets, and have keys and values:
Accessing items
• X=Dict[‘Name’]
• There is also a method called get() that will gives the same result:
x = Dict.get(“Name")
• Change value
Dict[“Name"] = ’Rahul’
• Loop through a dictionary
for x in Dict:
print(x)
13
07-05-2023
Operations on Dictionary
Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert
the key, with the specified value
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary
14