Python Lists



mylist = ["apple", "banana", "cherry"]

List

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:


Example

Create a List:

thislist = ["apple", "banana", "cherry"]
print(thislist)


List Items

List items are unique, flexible, and allow duplicate values.

List items are displayed, first item has reference [0], second item referenced [1] etc.


Ordered

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.



Changeable

The list is flexible, which means we can change, add, and remove items from the list once they have been created.


Allow Duplicates

As lists are identified, lists may contain items of the same value:


Example

Lists allow duplicate values:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)


List Length

To determine how many items are listed, use the len() function:


Example

Print the number of items in the list:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))


List Items - Data Types

List items can be for any type of data:


Example

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:


Example

A list with strings, integers and boolean values:

list1 = ["abc", 34, True, 40, "male"]


type()

From Python's point of view, lists are classified as 'data-bound':


<class 'list'>


Example

What is the data type of a list?

mylist = ["apple", "banana", "cherry"]
print(type(mylist))


The list() Cosntructor

It is also possible to use the list() builder when creating a new list.


Example

Using the list() constructor to make a List:

thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)


Python Collections (Arrays)

There are four types of data collection in Python editing language:

  • The list is a ordered and flexible collection. Allows duplicate members.
  • Tuple is a ordered and unchanged collection. Allows duplicate members.
  • Set is a random, unstable *, and unidentified collection. No duplicate members
  • The dictionary is a ordered order ** and is flexible. No duplicate members.

* 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.



Python - Access List Items



Access Items

List items are displayed and you can access them by referring to the reference number:


Example

Print the second item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Note: The first item has a 0 value.


Negative Indexing

Poor targeting means starting from the end

-1 refers to the last item, -2 refers to the last item etc.


Example

Print the last item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

Range of Indexes

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.


Example

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:


Example

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:


Example

This example returns the items from "cherry" to the end:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])


Range of Negative Indexes

Specify negative clues if you want to start a search from the end of the list:


Example

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])


Check if Item Exists

To determine if something is on the list, use a 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")



Python - Change List Items



Change item Value

To change the value of an item, see the reference number:


Example

Change the second item:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)


Change a Range of Item Values

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:


Example

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:


Example

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:


Example

Change the second and third value by replacing it with one value:

thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)


Insert Items

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:


Example

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.