List
List
Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities
and usage.
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.
Ordered
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.
Note: There are some list methods that will change the order, but in general:
the order of the items will not change.
Changeable
The list is changeable, meaning that 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:
Example
Lists allow duplicate values:
ADVERTISEMENT
List Length
To determine how many items a list has, use the len() function:
Example
Print the number of items in the list:
Example
String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
Try it Yourself »
Example
A list with strings, integers and boolean values:
type()
From Python's perspective, lists are defined as objects with the data type
'list':
<class 'list'>
Example
What is the data type of a list?
Example
Using the list() constructor to make a List:
ls1=[10,-4, 25, 13]
ls2=[“Tiger”, “Lion”, “Cheetah”]
List Operations / List Methods
There are several built-in methods in list class for various purposes. Here, we will discuss some
of them.
1. append(): ➢This method is used to add a new element at the end of a list.
➢Example: ls=[1,2,3] ls.append(„hi‟) ls.append(10) print(ls)
output: [5, 6, 1, 2, 3]
3. sort(): ➢This method is used to sort the contents of the list. By default, the function will sort
the items in ascending order.
➢Example : ls=[3,10,5, 16,-2]
ls.sort() print(ls)
output:[-2, 3, 5, 10, 16]
4. reverse(): ➢This method can be used to reverse the given list.
➢Example: ls=[4,3,1,6] ls.reverse()
5. count(): ➢This method is used to count number of occurrences of a particular value within
list.
➢Example ls=[1,2,5,2,1,3,2,10]
print(ls)
output: [6, 1, 3, 4]
ls.count(2)
output : 3 #the item 2 has appeared 3 tiles in ls
6. clear(): ➢This method removes all the elements in the list and makes the list empty.
➢Example ls=[1,2,3] ls.clear() print(ls) output: [ ]
➢Used to insert a value before a specified index of the list.
➢Example: ls=[3,5,10] ls.insert(1,"hi") print(ls)
7.insert():
output: [3, 'hi', 5, 10]
8. index(): ➢This method is used to get the index position of a particular value in the list.
➢Example: ls=[4, 2, 10, 5, 3, 2, 6]
ls.index(10) output: 3
9. pop(): ➢This method deletes the last element in the list, by default.
➢Example: ls=[3,6,-2,8,10]
x=ls.pop() #10 is removed from list and stored in x print(ls) output: [3, 6, -2, 8]
print(x) output :10
10. remove(): ➢When we don‟t know the index, but know the value to be removed, then this
function can be used.
➢Example: ls=[5,8, -12,34,2]
ls.remove(34) print(ls)
output: [5, 8, -12, 2]