python list

python lists

Python List

Python list is very flexible data structure in python. List can contain element of different types.

How to create list:
Python list is created by using square bracket ‘[]’
>>> my_list = []

The above list is blank, means there is no element present in it. list can be created with element also

>> my_list = [1, "ball", 2.5, "hello world", "cat"]
>>> my_list
[1, 'ball', 2.5, 'hello world', 'cat']

How to access list element:
you can access the list element by indexing. The indexing in python starts from zero to n-1 where n is total number of element in list. so in our list we have index -> 0,1,2,3,4
>> my_list[0]
>>> my_list[3]
'hello world'

We must provide the available index otherwise IndexError: list index out of range exception will be raised.
>>> my_list[5]
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range

Negative index
As index in python starts from 0 to n-1 where as negative index starts from -1 to -n.

[1, 'ball', 2.5, 'hello world', 'cat']  list with elements
-5   -4     -3        -2          -1   negative indexing

#when you want to access last element
>>> my_list[-1]

>>> my_list[-3]

List slicing
One can extract a part of list using slicing. list[start:end] will return a new list containing the element with the start index till end-1 index. see the example to understand.
>>> my_list
[1, 'ball', 2.5, 'hello world', 'cat']

>>> my_list[1:4]
['ball', 2.5, 'hello world']

#you don't have to provide start index when you including the 1st element.
>>> my_list[:3]
[1, 'ball', 2.5]

#reverse the list
>>> my_list[::-1]
['cat', 'hello world', 2.5, 'ball', 1]

# Start through end, skipping ahead 2 places each time.
>>> my_list[::2]
[1, 2.5, 'cat']

How to add or change element to list
Adding element to list we can use append, extend or insert method.
# append put the element at the end of list.
>>> my_list.append("apple")
>>> my_list
[1, 'ball', 2.5, 'hello world', 'cat', 'apple']

# insert(index, element) , puts the element at specified index.
>> my_list.insert(1,"bat")
>>> my_list
[1, 'bat', 'ball', 2.5, 'hello world', 'cat', 'apple']

# extend(list) , extend add content to existing list
>>> my_list = [1, 'ball', 2.5, 'hello world', 'cat']
>>> my_list.extend([1, 2, 3, 4])
>>> my_list
[1, 'ball', 2.5, 'hello world', 'cat', 1, 2, 3, 4]

Change element of list
>>> my_list = [1, 'ball', 2.5, 'hello world', 'cat']
>>> my_list[0] = "cow"
>>> my_list
['cow', 'ball', 2.5, 'hello world', 'cat']
>>> my_list[-1] = "dog"
>>> my_list
['cow', 'ball', 2.5, 'hello world', 'dog']

# series of element can be changed by slicing also
>>> my_list[1:4] = [1,2,3]
>>> my_list
['cow', 1, 2, 3, 'dog']

Delete or remove element from list
We can delete one or more items from a list using the keyword del. It can even delete the list entirely.
my_list = [1, 'ball', 2.5, 'hello world', 'cat']
>>> del my_list[0]
>>> my_list
['ball', 2.5, 'hello world', 'cat']

# delete multiple element
>>> del my_list[2:4]
>>> my_list
['ball', 2.5]

# delete entire list
>>> del my_list
>>> my_list
Traceback (most recent call last):
File "", line 1, in
NameError: name 'my_list' is not defined

We can use remove() method to remove the given item or pop() method to remove an item at the given index.
The pop() method removes and returns the last item if index is not provided. This helps us implement lists as stacks (first in, last out data structure).
We can also use the clear() method to empty a list

>>> my_list = [1, 'ball', 2.5, 'hello world', 'cat']
>>> my_list.remove(2.5)
>>> my_list
[1, 'ball', 'hello world', 'cat']

>>> my_list.pop()

>>> my_list.clear()
>>> my_list

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *