Python provides different data structures to store, modify, select, remove data. The most popular data structure is list
. A list is used to store sequential data in a single variable. Being sequential gives abilities like sort, list, add, index, loop, etc.
Python Array or List
Python lists are very similar and provide the same functionality of arrays in other languages like C, C++, Java … Here a list of the same functionalities shared by lists and arrays.
- Loop
- Index
- Enumeration
- 0 index start
Array or List Item
List consist of items. The item count is limited to system memory. Items have also some primitive data types like integer, string, object but all of these can be used as items.
Create List
Now we will create a simple list which is consists of variable types like string, integer. List items are enclosed with [ ]
and all items are put into square brackets. Items are separated with ,
. Below the syntax of a list can be seen.
[ item1 , 'item2' ]
In this example we will create a list and assign it into a variable which will be used list operations.
mylist = [ 'ismail' , 'ahmet' , 1 , 2 , 3 ]
Or we can create a list which consist of only integer items like below.
mynumbers= [ 1 , 2 , 3 , 4 ]
Or we can create a list which consist of only string items like below.
mystrings= [ 'ismail' , 'poftut' , 'ahmet' ]
We can create new examples like above.
Print All List Items
If we are working with the python interactive shell we can simply print the list issuing the list name like below. If we are writing script we can use print function providing list name as a parameter like below.
print(mynumbers)

Append or Add Item To The End Of The List
After creating a list we may need to add more items into the list. append
will add items to the list which is similar to the insert
but the difference is that item will be appended into the end of the list. In the following example we will add 6
to the end of the list named mynumbers
.
mynumbers.append(6)
mynumbers.append(7)

Insert or Add Item Into List At Specified Index
Insert operations are done with insert
function by providing the item we want to insert and the index number. Provided items will be inserted into the specified index and other items will be shifted. In this example, we insert item ‘ismail’ to the index number 3.
mynumbers.insert(3,'ismail')

Get or Return Item At Specified Index From List
In the previous example, we have seen that we can insert new items by specifying index. The index can be used to get and print related items. We will provide the index number in square brackets [index] in order to get items. In this example, we will get the item which is at 3
.
mynumbers[3]

Get or Return Multiple Items From Specified Range
In the previous example, we get single item but in some situations, we may need to get multiple items by specifying a range according to index. We will use [start:end] square brackets with the start and end of index numbers. In this example, we want to get a range between 2
and 5
.
mynumbers[2:5]

Multiple items are returned as list too. So we can assign returning list into new variable and use this list.
Remove Item From List
There are different types of removal operations on python lists. We will use remove
function by specifying the value of the item we want to remove. In this example, we will remove the item ismail
from the list.
mynumbers.remove('ismail')

Another way to remove an item is using del
keyword with the list name and index. In this example, we want to remove the index number 3
.
del mynumbers[3]

Pop or Select Last Item From List
In the previous example, we have removed items according to their values or index numbers. We can also remove or pop or select the last item and remove it from the given list by using pop function without providing any parameter like below. This will return the last item by removing it from the list.
mynumbers.pop()

Clear or Remove All Items of A List
Some times we need to clear or remove all items from the given list. This can be done one by one but it is a trivial task. We can use clear
function of the list to remove all items. But keep in mind that this will not undefine the list. The list will be an empty list.
mynumbers.clear()

Get Item Count of The Given List
As we stated in the beginning of this tutorial list may have a lot of items and the only restriction is the memory of the system. We can get the item count with len
keyword like below.
len(mynumbers)

Sort List Items
The list holds items in a shuffled or unsorted manner according to the adding sequence. Sometimes we may need the current list as a sorted format. This can be especially useful in lists where only item type is number. We can use sort for sorting the list. If we need we can provide more sophisticated sorting with sorted function. In this example, we have an unsorted list which consists of numbers between 0 and 10. We will sort them.
mynumbers.sort()

Reverse A List
In the previous example, we have sorted numeric list. We can also reverse the current order of the list. But keep in mind that this will not sort in reverse order. This will just reverse the current order.
mynumbers.reverse()

Copy Given List Into New List
Copying list can be done with copy
function. This copy operation is equivalent to a[:]
. In this example, we will copy list named myvariables
to the new list named myvariables2
.
mynumbers2 = mynumbers.copy()

Convert List To String
We can convert the list to string with join keyword by providing the delimiter and list name. But all items must be a string.
','.join(mylist)

List Comprehensions
Python lists provide a very useful feature named comprehension. Comprehension is a method to create list items by defining python statements that create sequential outputs. Explaining with examples is the best way. In this example, we will create a list that consists of odd numbers between 1 and 9. We will use range
function with for
loop.
oddnumbers=[2*i+1 for i in range(5)]
oddnumbers=[2*i+1 for i in range(5)]