Lists
• List is a sequence datatype and it is a collection of
elements which is ordered and changeable. Allows
duplicate members also.
• A Python list contains items separated by commas and
enclosed within square brackets ([]).
• One difference between them is that all the items
belonging to a Python list can be of different data type
where as C array can store elements related to same
data type.
• The values stored in a Python list can be accessed using
with index starting at 0 from beginning of the list and
the values stored in a Python list can be accessed
using with index starting at -1 from ending of the
list.
• The values stored in a Python list can be accessed using
the slice operator ([:]).
Tuples
• Python tuple is another sequence data type that is
similar to a list. Tuple is a collection of elements which
is ordered and unchangeable. Allows duplicate
members also.
• A Python tuple consists of a number of values separated
by commas. however, tuples are enclosed within
parentheses.
• The main differences between lists and tuples are: Lists
are enclosed in brackets ( [ ] ) and their elements and
size can be changed, while tuples are enclosed in
parentheses ( ( ) ) and cannot be updated.
• Tuples can be thought of as read-only lists.
Common Built-in Functions of Lists and Tuples in Python:
1. cmp(list1,list2) - It compares the elements of both lists,
list1 and list2
2. len(list) - It Returns length of string or set
3. max(list) - It Returns item that has Maximum value in a
list
4. min(list) - It Returns item that has Minimum value in a
list
5. list(seq) - It converts a tuple/set into a list
6. list.append(item) - It adds the item to the end of the list
7. list.count(item) - It returns number of times the item
occurs in a list
8. list.index(item) - It returns the index number of the item.
9. list.insert(index,item) - It inserts the given item on the the
given index number in the list
10 list.reverse()- It reverse the position(index number) of the
items in the list
11. list.sort() - It sorts the elements inside the list
12. list.extend(seq) - It adds the elements of the sequence at
the end of the list
SETS
A set is a collection of elements which are unordered,
unchangeable, and unindexed.
Sets are written with curly brackets (Braces).
Note: Set items are unchangeable i.e the items cannot
be changed after creating the set.
You can remove items from the sets and add new items
into the sets.
Sets are Not Allowed Duplicates Elements. i.e. Sets
cannot have two items with the same value.