Py08: List

A python list is used to store a sequence of data called elements. Each element is assigned with an integer known as an index. Index values start from zero, and increment by 1.

Declare List

Unlike an array, a list can consist of data from different data types. The list is indicated using a square. The list can add new elements and remove them.

Creating an empty list

list_name = [] 

Creating a list of items

list_name = [item1, item2,...., itemN]

For example, a list consisting of a Number

myList = [4, 5, 2, 6]
print(myList)

Output

[4, 5, 2, 6]
>

List consisting of characters

myList = ['J','o','e']
print("myList)

Output

['J', 'o', 'e']
>

List consisting Strings

myList = ['Adam','Ben Hong','Citra']
print(myList)

Output

['Adam', 'Ben Hong', 'Citra']
> 

A list consisting of mixed data types.

myList = ["Adam", 39, 89.5, 'A']
print("Original: ", myList)

Output

['Adam', 39, 89.5, 'A']
> 

Accessing List

Python uses index numbers to assess a specific element in the list. The first element is accessed with zero, the second is 1, and so on. Negative values allow element access from behind. -1 is the last element, -2 is the second last element, and so on...

list_name[index]

Example (hash shows a preview of the output)

myList = [4,5,2,6]

element1 = myList[0]
element2 = myList[1]
element3 = myList[2]
element4 = myList[3]

print("First element: ", element1)
print("Third element: ", element3)

Output

First element:  4
Third element:  2

If the index is out of range following error will occur

IndexError: list assignment index out of range

Modify Element

The index value is also used to change or modify the value of an element.

list_name[index] = newValue

Example

myList = [4,5,2,6]

print("Original: ", myList) 

myList[2] = 888

print("After modify: ", myList)

Output

Original:  [4, 5, 2, 6]
After modify:  [4, 5, 888, 6]
> 

If the index is out of range following error will occur

IndexError: list assignment index out of range

Add Element

To add a new element to the list, we can use the append() function.

list_name.append(value)

For example, add the number 888 to the list.

myList = [4, 5, 2, 6]
print("Original: ", myList)

myList.append(888)
print("After append: ", myList)

Output

Original: [4, 5, 2, 6]
After append: [4, 5, 2, 6, 888]
>

To add an item at the position specified.

list_name.insert(index, value)

For example, add the number 888 as the second element of the list.

myList = [4, 5, 2, 6]
print("Original: ", myList)

myList.insert(1, 888)
print("After append: ", myList)

Output

Original:  [4, 5, 2, 6]
After append:  [4, 888, 5, 2, 6]
>

Removing Element

To remove an element from a specific index, use the del operator.

del list_name[index]

Example

myList = [4, 5, 888, 2, 6]
print("Original: ", myList)

del myList[2] # removes the 3-rd item 
print("After delete: ", myList)

Output

Original:  [4, 5, 888, 2, 6]
After delete:  [4, 5, 2, 6]
> 

To remove an element based on value, use the remove() function.

list_name.remove(value)

Example

myList = [4, 5, 888, 2, 6]
print("Original: ", myList)

myList.remove(888)
print("After delete: ", myList)

Output

Original: [4, 5, 888, 2, 6]
After remove: [4, 5, 2, 6]
>

Next, the pop() the function removes an item from the end of a list and returns the item to the caller. Syntax:

last = list_name.pop()

Example

myList = [4, 5, 2, 6, 888]
print("Original: ", myList)

temp = myList.pop()
print("Popped: ", temp)
print("After first pop", myList)

temp = myList.pop()
print("Popped: ", temp)
print("After second pop", myList)

Output

Original:  [4, 5, 2, 6, 888]
Popped:  888
After first pop [4, 5, 2, 6]
Popped:  6
After second pop [4, 5, 2]
> 

the pop() function also takes an index as a parameter. It removes and returns the value of an item at the given index. Syntax:

item = list_name.pop(index)

Example

myList = [4, 5, 888, 2, 6]
print("Original: ", myList)

temp = myList.pop(2)
print("Popped: ", temp)
print("After first pop", myList)

temp = myList.pop(3)
print("Popped again: ", temp)
print("After second pop", myList)

Output

Original:  [4, 5, 888, 2, 6]
Popped:  888
After first pop [4, 5, 2, 6]
Popped again:  6
After second pop [4, 5, 2]
> 

Extend and (+) operator

The extend() function iterates over an iterable object like a list or tuple and adds each item to the list that invokes the extend function.

animals=['dog','cat']
newAnimals=['bird','zebra']

animals.extend(newAnimals)
print(animals)

Output

['dog', 'cat', 'bird', 'zebra']
>

Besides the append() function, another list can be appended using the plus (+) sign to append the list.

animals = ['dog','cat']

animals = animals + ['bird','hamster']
print(animals)

Output

['dog', 'cat', 'bird', 'hamster']
>

If a string is passed to the extend() function, each element is iterated and added one by one as the list item.

animals = ['dog','cat']

animals.extend('bird')
print(animals)

Output

['dog', 'cat', 'b', 'i', 'r', 'd']
>

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net