Python List Tutorial

The list is one of the most popular and useful data types in Python programming language. The Python list is used to store single or more items in a single variable. These items can be in different types like string, integer, floating-point number, etc even another list.

Define/Create A List

Lists are used to store a collection of the items in a single variables. Lists are generally created or defined by providing the list items inside the square brackets and delimiting them with commas. Comma is the list item separated and square brackets starts and ends the list.

names = ["ismail","ahmet","ali"]

numbers = [1,2,3,4,5]

items = ["ismail",1,2,3,"ali",4,"ahmet"]

The list can be created by using the list() constructer method which will accept the list items inside the parenthesis.

names = list(("ismail","ahmet","ali"))

numbers = list((1,2,3,4,5))

items = list(("ismail",1,2,3,"ali",4,"ahmet"))

Print List Items

Python provides the print() mehtod which wordks the list data type. Just provides the list name to the print() method which will be printed to the standard output.

names = ["ismail","ahmet","ali"]

numbers = [1,2,3,4,5]

items = ["ismail",1,2,3,"ali",4,"ahmet"]

Access List Items

The list is items are indexed which means numbered automatically. The index numbers start from 0 up to the last item. This makes things very easier and easy to access specific single or multiple or range of the items. As the first item is indexed as 0 we will use the index as 0. The index number is provided between square brackets

items = ["ismail",1,2,3,"ali",4,"ahmet"]

# Access first item
first_item = items[0]

# Access 3th item
first_item = items[2]

Update List Items

Existing items can be updated by using the index number of the item. This can be also called as replacement where specified index item will be set with the given value.

items = ["ismail",1,2,3,"ali",4,"ahmet"]

# Update first item
 = "newvalue"

# Update 3th item
 = 100

Add Item To List

The list is a dynamic type where new items can be added with the append() method. The new item is provided as a parameter to the append() method like below.

items = ["ismail",1,2,3,"ali",4,"ahmet"]

# Add new item

# Add new item

Delete List Items

Python provides the del statement which is used to delete specified objects. As the list items are objects too the del statement can be used to delete list items. The item should be specified with the index number.

items = ["ismail",1,2,3,"ali",4,"ahmet"]

# Delete first item
del items[0]

# Delete 4th item
del items[3]

Alternatively, the remove() method of the list can be used to remove or delete a specific item. The item should be provided to the remove() method which will be deleted.

items = ["ismail",1,2,3,"ali",4,"ahmet"]


List Repetition

Lists can be multiplied which is called as list repetition where given list can be repeated at specified time. The multiply operator * will be used for the list repetition.

items = [1,2,3,4]


Check If Item Exist In List

As a list can contain multiple items we may need to check if a specified item exists in a list. The in keyword is used to check item existence. In the following example, we will check if 3 exist in the list named items. This operation is also called as membership check.

items = [1,2,3,4]

if(3 in items):
   print("3 exist in items")

if(3 in [1,2,3,4]):
   print("3 exist in the list")

Reverse List

List stores the items according to their provided order. Current order of the list can be reversed by using the reverse() method.

items = [1,2,3,4]


Index of List Item

Th index number is very important and regularly used in list. The index number of the specific list item can be found with the index() method by providing the item as parameter.

numbers = [1,2,3,4]


Concatenate Lists

Two or more lists can be concatenated to each other. This will return single list which contains contatenated lists items. This can be also called as joining lists. If the same item exist in two or more lists there will nor merged.

numbers = [1,2,3,4]

names = ["ismail","ahmet","ali"]

names_numbers = names + numbers


List Slicing

List slicing mean extracting some part of the list by specifying or implying the index numbers. List slicing is one of the most popular and powerfull feature of the Python list.

numbers = [1,2,3,4]




List Length

As a dynamic tye a list may contain different number of elements. The python provides the len() method in order to return the size of the given data type which will return item count for a list variable.

items = ["ismail",1,2,3,"ali",4,"ahmet"]

items_length = len(items)

Iterate Over List

The list is an iterable type where items in the list can be iterated with for loop. In every iteration step an item will be enumerated into the specified variable.

items = [1,2,3,4]

for x in items:

Leave a Comment