Python Tutorials
Python File Handling
Python Modules
mylist = ["apple", "banana", "cherry"]
The list is used to store multiple items in one place.
The list is one of 4 data types built into Python that are used to store data collections, the other 3 are Tuple, Set, and Dictionary, all with different capabilities and usage.
Listing is done using square brackets:
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List items are unique, flexible, and allow duplicate values.
List items are displayed, first item has reference [0]
, second item referenced [1]
etc.
If we say the lists are separate, it means that the items have a specific order, and that order will not change.
When you add new items to the list, new items will be placed at the end of the list.
Note: There are other listing methods that will change the order, but generally: the order of things will not change.
The list is flexible, which means we can change, add, and remove items from the list once they have been created.
As lists are identified, lists may contain items of the same value:
Lists allow duplicate values:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
To determine how many items are listed, use the len()
function:
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
List items can be for any type of data:
String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
The list may contain various types of data:
A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]
From Python's point of view, lists are classified as 'data-bound':
<class 'list'>
What is the data type of a list?
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
It is also possible to use the list()
builder when creating a new list.
Using the list()
constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
There are four types of data collection in Python editing language:
* Set items do not change, but you can delete and / or add items whenever you wish.
** Starting with version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.
When choosing a type of collection, it is helpful to understand the characteristics of that type. Choosing the right type of specific data set may mean keeping the definition, and, it may mean increased efficiency or security.
List items are displayed and you can access them by referring to the reference number:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Note: The first item has a 0 value.
Poor targeting means starting from the end
-1
refers to the last item, -2 refers to the last item etc.
Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
You can specify the width of the indicators by specifying where to start and where to end the width.
When you specify a range, the return value will be the new list containing the specified items.
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]
print(thislist[2:5])
Note: Search will start in index 2 (included) and end in index 5 (not included).
Remember that the first item has a 0 value.
By excluding the original value, the range will start at the first item:
This example returns the items from the beginning to, but NOT including, "kiwi":
thislist = ["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]
print(thislist[:4])
Excluding the last value, the range will continue to the end of the list:
This example returns the items from "cherry" to the end:
thislist = ["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]
print(thislist[2:])
Specify negative clues if you want to start a search from the end of the list:
This example returns the items from "orange" (-4) to, but NOT including "mango" (-1):
thislist = ["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]
print(thislist[-4:-1])
To determine if something is on the list, use a in
keyword:
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
To change the value of an item, see the reference number:
Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
To change the value of items between a particular range, define a list with new values, and then specify a reference number range where you want to enter new values:
Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
If you enter more items than you change, new items will be added as you specify, and the remaining items will go accordingly:
Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant",
"watermelon"]
print(thislist)
Note: The length of the list will change when the number of items included does not match the number of items changed.
If you enter smaller items than you replace, new items will be added as you specify, and the remaining items will go accordingly:
Change the second and third value by replacing it with one value:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
To add a new list item, without adding any existing values, we can use the insert()
method.
The insert()
method inserts an object into a specified index:
Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
Note: As a result of the example above, the list will now contain 4 items.