Python provides different data structures to store, modify, select, remove data. The most popular data structure is
list. List is used to store sequential data in a single variable. Being sequential gives abilities like sort, list, add, index, loop etc.
Python lists are very similar and provides same functionality of arrays in other languages like C, C++, Java … Here a list of same functionalities shared by lists and arrays
- 0 index start
List consist of items. The item count is limited with system memory. Items have also some primitive data type like integer, string, object but all of these can be used as item.
Now we will create a simple list which is consist 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.
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
Append or Add 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
Insert or Add 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 item will be inserted into specified index and other items will be shifted.
In this example we insert item
'ismail' to the index number 3 .
Get Item At Specified Index
In previous example we have seen that we can insert new items by specifying index. Index can be used to get and print related items. We will provide the index number in square brackets
[index] in order to get item. In this example we will get the item which is at
Get Multiple Items At Specified Range
In 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 start and end of index numbers. In this example we want to get range between
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 type of removal operations on python lists. We will can 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.
Another way to remove an item is using
del keyword with list name and index. In this example we want to remove the index number
Pop or Select Last Item
In previous example we have removed items according to their values or index numbers. We can also remove or pop or select last item and remove from given list by using
pop function without providing any parameter like below. This will return last item by removing it from the list.
Clear or Remove All Items
Some times we need to clear or remove all items from given list. This can be done one by one but it is 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.
Get Item Count
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.
Sort List Items
List holds items in a shuffled or unsorted manner according to the adding sequence. Sometimes we may need 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 consist of numbers between 0 and 10. We will sort them.
In 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.
Copying list can be done with
copy function. This copy operations is equivalent to
a[:] . In this example we will copy list named
myvariables to the new list named
mynumbers2 = mynumbers.copy()
Convert List To String
We can convert list to string with
join keyword by providing the delimiter and list name.But all items must be string.
Python lists provides very useful feature named comprehensions. Comprehension is method to create list items by defining python statements those creates sequential outputs. Explaining with examples is the best way.
In this example we will create a list which consist of odd numbers between 1 and 9 . We will use
range function with
oddnumbers=[2*i+1 for i in range(5)]