Previous | Next


List is one of the Python’s built-in types of sequences. When you create a list in Python, the interpreter creates an array-like data structure in memory to hold your data, with your data items stacked from the bottom up. Like array technology in other programming languages, the first slot in the stack is numbered 0, the second is numbered 1, the third is numbered 2, and so on.

An empty list:

The empty list is written as a square brackets containing nothing.
>>> numbers = []

To create a list:

Create list by separating the data with a comma in a square brackets.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

To print all items in a list:

Use for loop to print each element in a list.
for num in numbers:
  print num

Modify item’s content based on its index in a list:

Use index to access and modify element in a list.
>>> numbers[2] = 5  # change 2 to become 5
>>> numbers
[0, 1, 5, 3, 4, 5, 6, 7, 8, 9]

To get the length of a list:

Use len() to get the length of a tuple.
>>> len(numbers)
10

To add new item to the end of a list:

Use append() to add new item to the tail in a list.
>> numbers.append(11) # insert 11 to the tail of list
>> numbers
[0, 1, 5, 3, 4, 5, 6, 7, 8, 9, 11]

To remove item from the end of a list:

Use pop() to remove the tail item in a list.
>> numbers.pop() # remove 11 from the tail of list
11

To add a collection of data items to the end of a list:

Use extend() to add more than one data item to the tail in a list.
>>> numbers.extend([12, 13]) # insert 12 and 13 to the tail of list
>>> numbers
[0, 1, 5, 3, 4, 5, 6, 7, 8, 9, 12, 13]

To remove a specific data item from a list:

Use remove() to remove item in a specific location.
>>> numbers.remove(12) # remove 12 from list
>> numbers
[0, 1, 5, 3, 4, 5, 6, 7, 8, 9, 13]

To add a data item before a specific slot location in a list:

Use insert() to insert new item in a specific location.
>>> numbers.insert(5, 8) # insert 8 to index position 5
>>> numbers
[0, 1, 5, 3, 4, 8, 5, 6, 7, 8, 9, 13]

To counts the occurrences of an element in a list:

Use count() to return number of occurrences of value.
>>> numbers.count(8)
2

To get the index of an item in a list:

Use index() to get the index position in a list.
>>> numbers.index(5)
2

To reverse the items in a list:

Use reverse() to reverse the items order in a list.
>> numbers.reverse()
>>> numbers
[13, 9, 8, 7, 6, 5, 8, 4, 3, 5, 1, 0]

To sort the items in a list:

User sort() to sort the item order in a list.
>>> numbers.sort()
>>> numbers
[0, 1, 3, 4, 5, 5, 6, 7, 8, 8, 9, 13]

To check whether a value can be found in a list:

Use ‘in’ membership operator to check the existence of item in a list.
>>> 5 in numbers
True

To access ranges of elements in a list:

Use slice() to access more than one data item in a list.
>>> numbers[3:6] #accesses the items from index location 3 up-to-but-not-including index location 6.
[4, 5, 5]
>>> numbers[2:4] #accesses the items from index location 2 up-to-but-not-including index location 4.
[3, 4]
>>> numbers[0:3] #accesses the items from index location 0 up-to-but-not-including index location 3.
[0, 1, 3]
>>> numbers[:3] #accesses the items from index location 0 up-to-but-not-including index location 3.
[0, 1, 3]
>>> numbers[3:] #accesses the items from index location 3 up-to last index location.
[4, 5, 5, 6, 7, 8, 8, 9, 13]

To create a nested list:

It is allow to contains another list within a list. In the following example, numbers will contain of 2 sub lists [0, 1, 3, 4, 5, 5, 6, 7, 8, 8, 9, 13] and [1, 2, 3].
>>> numbers = [numbers, [1,2,3]]
>>> numbers
[[0, 1, 3, 4, 5, 5, 6, 7, 8, 8, 9, 13], [1, 2, 3]]

Previous | Next