4. List
A container data type that stores a sequence of elements. Unlike
strings, lists are mutable: modification possible. And its
Applications are:
• Used in JSON format
• Useful for Array operations
• Used in Databases
5. List
• 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.
6. 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.
There are some list methods that will change the order, but in general:
the order of the items will not change.
7. Changeable
We can change, add, and remove items in a list after it
has been created
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
9. List Methods
Lists also get various methods to perform
all operations on list items that called that
methods as “ .method’s name() “ such as
append, remove, pop, delete, insert etc..
You can see all methods called “ dir(list_name)”
10. All Methods of List Object
1. List_name.append(x)
Add an item to the end of the
list. Equivalent to
List_name[len(List_name):] = [x]
11. All Methods of List Object
2. List_name.extend(iterable)
Extend the list by appending all the
items from the iterable.
Equivalent to
List_name[len(List_name):] = iterable
12. All Methods of List Object
3. List_name.insert(i, x)
Insert an item at a given position. The first argument is the
index of the element before which to insert, so
List_name.insert(0, x) inserts at the front of the list, and
List_name.insert(len(a), x) is equivalent to List_name.append(x)
13. All Methods of List Object
4. List_name.remove(x)
Remove the first item from the list whose value is equal to x
It raises a ValueError if there is no such item.
14. All Methods of List Object
4. List_name.pop([i])
Remove the item at the given position in the list
and return it. If no index is specified, List_name.pop()
removes and returns the last item in the list.
(The square brackets around the i in the method
signature denote that the parameter is optional, not
that you should type square brackets at the
position.)
15. All Methods of List Object
5. List_name.clear()
Remove all items from the list.
Equivalent to del List_name[:]
16. All Methods of List Object
6. List_name.index(x[, start[, end]])
Return zero-based index in the list of the first item whose
value is equal to x. Raises a ValueError if there is no such
item.
The optional arguments start, and end are interpreted as
in the slice notation and are used to limit the search to a
particular subsequence of the list. The returned index is
computed relative to the beginning of the full sequence
rather than the start argument.
17. All Methods of List Object
7. List_name.count(x)
Return the number of
times x appears in the list.
18. All Methods of List Object
8. List_name.sort(*, key=None, reverse=False)
Sort the items of the list in place (the
arguments can be used for sort
customization.
19. All Methods of List Object
9. List_name.reverse()
Reverse the elements of the list in place.
20. All Methods of List Object
9. List_name.copy()
Return a shallow copy of the list.
Equivalent to List_name[:]
21. Tuple
• Tuples are Ordered, Unchangeable, allow Duplicate.
• Tuples are created by brackets ()
22. Ordered
Each items on Tuples has an index number.
Allow Duplicates
Since Tuples are indexed, they can have items with the same value.
Unchangeable
Any items on Tuples could not be removed, added, changed.
24. All Methods of List Object
1. tuple_name.index(x[, start[, end]])
Searches the tuple for a specified
value and returns the position of
where it was found.
25. All Methods of Tuple Object
2. tuple_name.count()
Returns the number of
times a specified value
occurs in a tuple
26. Append two tuples
The addition operation simply
performs a concatenation with
tuples.
You can only add or combine same data
types. Thus, combining a tuple and a list
gives you an error.
27. Removing items from a tuples
Tuples are immutable. You can't
append to a tuple.
28. Dictionary
• 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.
29. Ordered
Each items on Dictionaries have a defined order, and that order will not change.
Not Allow Duplicates
Dictionaries cannot have two items with the same key.
changeable
we can change, add or remove items after the dictionary has been created.
37. All Methods of Dictionary Object
1. dic_name.get(key)
Get the value of a certain key.
38. All Methods of Dictionary Object
2. dic_name.keys()
Get a list of the keys.
3. dic_name.Values()
This method will return a
list of all the values in the
dictionary.
4. dic_name.items()
method will return each
item in a dictionary, as
tuples in a list.
39. All Methods of Dictionary Object
5. dic_name.get(key)
Get the value of a certain key.
40. All Methods of Dictionary Object
Removing Items:
There are several methods to
remove items from a dictionary:
6. dic_name.pop(key)
The pop() method removes the
item with the specified key name
41. All Methods of Dictionary Object
7. dic_name.popitem()
The popitem() method removes the last
inserted item
(in versions before 3.7, a random item is
removed instead)
42. All Methods of Dictionary Object
7. del dic_name[key]
The del keyword removes the item with the
specified key name
8. del dic_name
It can also delete the dictionary completely
43. All Methods of Dictionary Object
8. dic_name.clear()
The clear() method empties
the dictionary
44.
45. All Methods of Dictionary Object
Copy a dictionary:
8. dic_name.copy()
Make a copy of a dictionary with the copy() method
9. dic_name2 = dict(dic_name1)
Make a copy of a dictionary with the dict() function
46. Nested dictionary
A dictionary can contain dictionaries, this is called nested dictionaries.
To access items from a nested dictionary, you use the name of the dictionaries, starting with
the outer dictionary:
47. Set
• Sets are used to store
multiple items in a
single variable.
• Set items are
unordered,
unchangeable, and do
not allow duplicate
values.
48. Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them and cannot be
referred to by index or key.
Not Allow Duplicates
Sets cannot have two items with the same value.
Unchangeable
Set items are unchangeable, meaning that we cannot change the
items after the set has been created.
49. Different data types
• Set items can be of any data type:
• A set can contain different data types:
50. The set() Constructor
It is also possible to use the set() constructor to make a set.
Once a set is created, you cannot change its items, but you can add new items.
52. The values True and 1, False and 0 are
considered the same value in sets, and
are treated as duplicates
53. Access Items
You cannot access items in a set by referring to an index or a key.
But you can loop through the set items using a for loop or ask if a specified value is
present in a set, by using the in keyword.
54. Add Items
• To add one item to a set, use the add() method
55. Add Sets
• To add items from another set into the current set, use
the update() method.
The object in the update() method does not have to be a set, it can be any iterable
object (tuples, lists, dictionaries etc.).
56. Remove Item
To remove an item in a set, use the remove(), or the
discard() method.
Note: If the item to remove does not exist, remove() will raise an error, but discard()
will NOT raise an error.
57. Pop an item
Pop() method to remove an item, but this method will
remove a random item, so you cannot be sure what item that
gets removed.
The return value of the pop() method is the removed item.
Note: Sets are unordered, so when using the pop() method, you do not know which
item that gets removed.
58. Clear and delete
The clear() method empties the set:
The del keyword will delete the set completely
59. Join Sets
There are several ways to join two or more sets in Python.
You can use the union() method that returns a new set
containing all items from both sets, or the update() method
that inserts all the items from one set into another
The union() method returns a new set with all items from
both sets
Note: Both union() and update() will exclude any duplicate items.
60. Keep ONLY the Duplicates
The intersection_update() method will keep only the
items that are present in both sets.
The Intersection() method will return a new set,
that only contains the items that are present in
both sets.
61. Keep All, But NOT the Duplicates
The symmetric_difference_update() method will keep
only the elements that are NOT present in both sets.
The symmetric_difference()
method will return a new set, that
contains only the elements that are
NOT present in both sets.
62. All Methods of set Object
Description
Method
Adds an element to the set
add()
Removes all the elements from the set
clear()
Returns a copy of the set
copy()
Returns a set containing the difference between two or more sets
difference()
Removes the items in this set that are also included in another, specified set
difference_update()
Remove the specified item
discard()
Returns a set, that is the intersection of two other sets
intersection()
Removes the items in this set that are not present in other, specified set(s)
intersection_update()
Returns whether two sets have a intersection or not
isdisjoint()
Returns whether another set contains this set or not
issubset()
Returns whether this set contains another set or not
issuperset()
Removes an element from the set
pop()
Removes the specified element
remove()
Returns a set with the symmetric differences of two sets
symmetric_difference()
inserts the symmetric differences from this set and another
symmetric_difference_update()
Return a set containing the union of sets
union()
Update the set with the union of this set and others
update()