Python List Tutorial

The list is one of the most popular and useful data types in the 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 List

Lists are used to store a collection of items in a single variable. Lists are generally created or defined by providing the list items inside the square brackets and delimiting them with commas. The comma is the list item separated and square brackets start and end 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() method which works with 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"]
print(names)

numbers = [1,2,3,4,5]
print(numbers)

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

Access List Items

The list of items is 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 a specific single or multiple ranges of 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 a replacement where a specified index item will be set with the given value.

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

# Update first item
items[0] = "newvalue"

# Update 3th item
items[2] = 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
items.append("newvalue")

# Add new item
items.append(100)

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

items.remove("ismail")

List Repetition

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

items = [1,2,3,4]

print(items*3)

Check If Item Exists 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 of named items. This operation is also called a 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. The current order of the list can be reversed by using the reverse() method. Items are reversed by their index, not their values.

items = [1,2,3,4]

print(items.sort())

Index of List Item

The index number is very important and regularly used in the list. The index number of the specific list item can be found with the index() method by providing the item as a parameter. The index numbers start from 0 and increased one by one by default.

numbers = [1,2,3,4]

print(numbers.index(3))

Concatenate Lists

Two or more lists can be concatenated with each other. This will return a single list that contains concatenated list items. These can be also called joining lists. If the same item exists in two or more lists there will be no merger.

numbers = [1,2,3,4]

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

names_numbers = names + numbers

print(names_numbers)

List Slicing

List slicing means extracting some part of the list by specifying or implying the index numbers. List slicing is one of the most popular and powerful features of the Python list.

numbers = [1,2,3,4]

print(numbers[1:])

print(numbers[1:2])

print(numbers[:2])

List Length

As a dynamic type, a list may contain a different number of elements. Python provides the len() method in order to return the size of the given data type which will return the 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:
   print(x)

Leave a Comment