Lists in Python
XI Science
Computer Science
What is a list?
A list is a collection that may contain elements of different
data types like Integers, Strings and Objects. Lists in python
are created using square brackets.
Lists are mutable, that is it can be altered even after their
creation.
The elements in a list are indexed according to a definite
sequence and the indexing of a list begins with 0.
thislist = ["apple", "banana", "cherry"]
print(thislist)
Output:
['apple', 'banana', 'cherry']
Accessing list Items
We access the list items by referring to the index number:
Example
Output:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
banana
print(thislist[1])
Accessing list Items
We access the list items by referring to the index number:
Example
Output:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
banana
print(thislist[1])
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item,
-2 refers to the second last item etc.
Example Output:
Print the last item of the list:
thislist = ["apple", "banana", "cherry"] cherry
print(thislist[-1])
Range of Indexes
We 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 list with the
specified items.
Example
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Output:
['cherry', 'orange', 'kiwi']
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to "orange":
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
Output:
['apple', 'banana', 'cherry', 'orange']
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to "orange":
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
Output:
['apple', 'banana', 'cherry', 'orange']
By leaving out the end value, the range will go on to the end of the list:
Example
This example returns the items from "cherry" and to the end:
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
Output:
['cherry', 'orange', 'kiwi', 'melon',
'mango']
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the list:
Example
This example returns the items from index -4 (included) to index -1 (excluded)
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Output:
['orange', 'kiwi', 'melon']
Change Item Value
To change the value of a specific item, we should refer the index number and assign
the new element.
For Example
To change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Output:
['apple', 'blackcurrant', 'cherry']
Loop Through a List
We can loop through the list items by using a for loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Output:
apple
banana
cherry
Check if Item Exists
To determine if a specified item is present in a list use the in keyword:
Example
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
Output:
Yes, 'apple' is in the fruits list
Add Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
['apple', 'banana', 'cherry', 'orange']
Add Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
['apple', 'banana', 'cherry', 'orange']
To add an item at the specified index, use the insert() method:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
['apple', 'orange', 'banana', 'cherry']
Remove Item from a list
There are three methods to remove items from a list:
1.Using remove() function
2. Using pop() function
3. Using del statement
Remove Item from a list
There are three methods to remove items from a list:
1.Using remove() function
2. Using pop() function
3. Using del statement
The remove() method removes the specified item: ['apple',
thislist = ["apple", "banana", "cherry"] 'cherry']
thislist.remove("banana")
print(thislist)
Remove Item from a list
There are three methods to remove items from a list:
1.Using remove() function
2. Using pop() function
3. Using del statement
The remove() method removes the specified item: ['apple',
thislist = ["apple", "banana", "cherry"] 'cherry']
thislist.remove("banana")
print(thislist)
The pop() method removes the specified index, (or the last
item if index is not specified): ['apple', 'banana']
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
Remove Item from a list
There are three methods to remove items from a list:
1.Using remove() function
2. Using pop() function
3. Using del statement
The remove() method removes the specified item: ['apple',
thislist = ["apple", "banana", "cherry"] 'cherry']
thislist.remove("banana")
print(thislist)
The pop() method removes the specified index, (or the last
item if index is not specified): ['apple', 'banana']
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
The del keyword removes the specified index:
thislist = ["apple", "banana", "cherry"]
del thislist[0] ['banana', 'cherry']
print(thislist)