AI and ML with
Python
Chapter 4 : Python Collections
Chapter 4:
Python Collections
What are Python Collections?
● Used to store collections of data in a single variable.
● Also called as containers of data, python collections can
be used to store different types of data from
numbers,strings and a combination of both.
● Some examples of python collections are:
■ Collection of total marks in all subjects by the
student.
■ Collection of food items offered by a restaurant.
Some Important Python Collections:
● Lists
● Tuples
● Sets
● Dictionaries
Python Lists
● List is used to store multiple values in a single variable.
Python Lists: Finding Length
● Length of a python list can be found by using the python
len() function.
Python Lists: Access List Item at Specific
Index
● We can access list item at specific index by passing there
index name inside [ ] to the list.
Python Lists: Update List Item at Specific
Index
● We can access list item at specific index by passing there
index name inside [ ] to the list.
Python Lists: Insert Item to the List.
● We can insert item at any position using insert() method.
Python Lists: Append Item to the List.
● We can insert item to the end of the list by using append()
method.
Python Lists: Remove Items from List
● We can delete item from the list by specifying its value to
remove() method.
Python Lists: Remove Item By Index
● We can delete item from the list by specifying its index
position to pop() method.
Python Lists: Sort()
● We can sort the lists in ascending or descending order.
Python Lists: Copy()
● We can copy the lists in another list.
Python Lists: Join Lists
● Two lists can be joined in python using + operator.
Python Lists: Delete all Items
● We can delete all items by calling clear() method on the list.
Features of Python Lists
● List items can be changed.
● Allows to store duplicate values.
● All items are indexed which start with 0.
Python Tuples:
● Tuples store multiple values.
● We cannot change, add or remove once a tuple is created.
● It allows duplicate values.
Example:
Python Sets:
● Sets store multiple values.
● Sets are written with curly Brackets.
● They are unordered and unindexed.
● Duplicate values are ignored in Sets.
Example:
Python Dictionaries:
● Stores data values in key and pairs.
● Unordered data.
● Does Not allow duplicates.
● Data is changeable unlike sets and tuples.
Example:
Python Collections: Comparison
Thank You