LIST in PYTHON
Lists are used to store multiple items in a single variable. Lists are created using square brackets. List
items can be of any data type. A list can contain different data types.
Create a List:
Example: L1 = ["apple", "banana", "cherry"]
print(L1)
List items are ordered, changeable, and allow duplicate values.
When we say that lists are 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. The list is
changeable, meaning that we can change, add, and remove items in a list after it has been created.
Since lists are indexed, lists can have items with the same value.
Change Item Value
To change the value of a specific item, refer to the index number:
Change the second item:
list = ["apple", "banana", "cherry"]
list[1] = "blackcurrant"
print(list)
Change a Range of Item Values
Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
list = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
list[1:3] = ["blackcurrant", "watermelon"]
print(list)
List Methods:
Python has a set of built-in methods that you can use on lists.
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
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Append()
To add an item to the end of the list, use the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Insert()
To insert a list item at a specified index, use the insert() method. The insert() method inserts an item
at the specified index:
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Extend List
To append elements from another list to the current list, use the extend() method.
Add the elements of tropical to thislist:
list = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
list.extend(tropical)
print(list)
Remove Specified Item
The remove() method removes the specified item.
Remove "banana":
list = ["apple", "banana", "cherry"]
list.remove("banana")
print(list)
Remove Specified Index
The pop() method removes the specified index. If you do not specify the index, the pop() method
removes the last item. The del keyword also removes the specified index.
Remove the second item:
list = ["apple", "banana", "cherry"]
list.pop(1)
print(list)
del keyword
The del keyword can also delete the list completely.
list = ["apple", "banana", "cherry"]
del list
Clear the List
The clear() method empties the list.
The list still remains, but it has no content.
list = ["apple", "banana", "cherry"]
list.clear()
print(list)
Sort List
List objects have a sort() method that will sort the list alphanumerically, ascending, by default:
Example
list = ["orange", "mango", "kiwi", "pineapple", "banana"]
list.sort()
print(list)
Sort Descending
To sort descending, use the keyword argument reverse = True:
list = ["orange", "mango", "kiwi", "pineapple", "banana"]
list.sort(reverse = True)
print(list)
Reverse Order
Reverse the order of the list items:
list = ["banana", "Orange", "Kiwi", "cherry"]
list.reverse()
print(list)
TUPLE in PYTHON
Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered
and unchangeable. When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change. Tuples are unchangeable, meaning that we cannot change, add
or remove items after the tuple has been created. They can have items with the same value. A tuple
can contain different data types. Tuples are written with round brackets.
Ex: Create a Tuple:
Tuple = ("apple", "banana", "cherry")
print(Tuple)
Tuple items are ordered, unchangeable, and allow duplicate values.
Join Two Tuples
To join two or more tuples you can use the + operator:
Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use the * operator:
Example
Multiply the fruits tuple by 2:
fruits = ("apple", "banana", "cherry")
tuple = fruits * 2
print(tuple)
Tuple Methods:
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found
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. You can convert the tuple into a list, change the list, and convert the list
back into a tuple.
EX: Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
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.
Convert the tuple into a list, add "orange", and convert it back into a tuple:
tuple = ("apple", "banana", "cherry")
y = list(tuple)
y.append("orange")
tuple = tuple(y)
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:
EX: Create a new tuple with the value "orange", and add that tuple:
tuple = ("apple", "banana", "cherry")
y = ("orange",)
tuple += y
print(tuple)
Remove Items
Tuples are unchangeable, so you cannot remove items from it, but you can use the same
workaround as we used for changing and adding tuple items:
Convert the tuple into a list, remove "apple", and convert it back into a tuple:
tuple = ("apple", "banana", "cherry")
y = list(tuple)
y.remove("apple")
tuple = tuple(y)