Python List or Array Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-list-or-array-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

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.

Array

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

  • Loop
  • Index
  • Enumeration
  • 0 index start

Item

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.

Create List

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.

In this example we will create a list and assign it into a variable which will be used list operations.

Or we can create a list which consist of only integer items like below.

Or we can create a list which consist of only string items like below.

We can create new examples like above.

Print List

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 parameter like below.

LEARN MORE  Javascript Regular Expression Operation

Print List
Print List

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

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 .

Insert Into List
Insert Into List

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

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 2 and 5 .

Get Multiple Items At Specified Range
Get Multiple Items At Specified Range

Multiple items are returned as list too. So we can assign returning list into new variable and use this list.

LEARN MORE  Powershell Trim Operation Like Filename, String, End, Start, Spaces

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.

Remove Item From List
Remove Item From 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 3 .

Remove Item From List
Remove Item From List

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.

Pop or Select Last Item
Pop or Select Last Item

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.

Clear or Remove All Items
Clear or Remove All Items

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.

Get Item Count
Get Item Count

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.

LEARN MORE  Php - String Variable Type

Sort List Items
Sort List Items

Reverse List

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.

Reverse List
Reverse List

Copy List

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 myvariables2

Copy List
Copy List

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.

Convert List To String
Convert List To String

List Comprehensions

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 for loop.

List Comprehensions
List Comprehensions
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-list-or-array-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.