List in Python
List in Python
Page 1
List
List Items
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.
Allow Duplicates
List Length
type()
Negative Indexing
Python Lists & Tuple
Page 3
Range of Indexes
Insert Items
To insert a list item at a specified index, use the insert() method.
Extend List
To append elements from another list to the current list, use the extend() method.
Use the len() function to determine the length of the list, then start at 0 and loop your
way through the list items by referring to their indexes.
Sort Descending
To sort descending, use the keyword argument reverse = True:
Reverse Order
What if you want to reverse the order of a list, regardless of the alphabet?
Python Lists & Tuple
Page 10
The reverse() method reverses the current sorting order of the elements.
Copy a List
You 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().
Python Tuples
mytuple = ("apple", "banana", "cherry")
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Python Lists & Tuple
Page 13
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and
that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the
tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
Tuple Length
To determine how many items a tuple has, use the len() function:
type()
From Python's perspective, tuples are defined as objects with the data type 'tuple':
<class 'tuple'>
Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range.
When specifying a range, the return value will be a new tuple with the specified items.
But there is a workaround. You can convert the tuple into a list, change the list, and
convert the list back into a tuple.
Python Lists & Tuple
Page 17
Add Items
Since tuples are immutable, they do not have a built-in append() method, but there are
other ways to add items to a tuple.
1. Convert into a list: Just like the workaround for changing a tuple, you can convert it
into a list, add your item(s), and convert it back into a tuple.
2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to
add one item, (or many), create a new tuple with the item(s), and add it to the existing
tuple:
Python Lists & Tuple
Page 18
Remove Items
Packing a tuple
Unpacking a tuple
Use the len() function to determine the length of the tuple, then start at 0 and loop your
way through the tuple items by referring to their indexes.
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use
the * operator: