Skip to content

Python Lists Tutorial and Examples

  • by

Python Lists, List literals are written within square brackets [ ]. A list is a data structure in Python that is a mutable (changeable), ordered sequence of elements. Python List stores data in a bracket with comma-separated. It’s one of the most frequently used and very versatile data types used in Python.

Python Lists Tutorial and Examples of Improtant Functions

A list is a collection which is ordered and changeable. In Python, lists are written with square brackets [ ].

Simple Example Python Lists

Creating a list is as simple as putting different comma-separated values between square brackets. Python lists indexing starts from 0. Here is an example, how to create a list in python and print it in the console.

colorList = ["Red", "Yellow", "green"]
print(colorList)
list1 = ['School', 'MMKO', 2000];
print(list1)

Output : [‘Red’, ‘Yellow’, ‘green’]

[‘School’, ‘MMKO’, 2000]

Example: Print Single item or Select any only by Index number

colorList = ["Red", "Yellow", "green"]
print(colorList[1])

Output: Yellow

Python Lists Functions

Here are some important list function with examples, it’s great to have.

  • list.append(elem) – adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
colorList = ["Red", "Yellow", "green"]
print(colorList)
colorList.append("Black")
print(colorList)

Output : [‘Red’, ‘Yellow’, ‘green’]
[‘Red’, ‘Yellow’, ‘green’, ‘Black’]

  • list.insert(index, elem) – inserts the element at the given index, shifting elements to the right.
colorList = ["Red", "Yellow", "Green"]
print(colorList)
colorList.insert(2, "Black")
print(colorList)

Output : [‘Red’, ‘Yellow’, ‘Green’]
[‘Red’, ‘Yellow’, ‘Black’, ‘Green’]

  • list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
list1 = [1, 2, 3]
list2 = [8, 5, 6]
list1.extend(list2)
print(list1)

Output : [1, 2, 3, 8, 5, 6]

  • list.index(elem) : Searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear
colorList = ["Red", "Yellow", "Green"]
print(colorList)
print(colorList.index("Yellow"))

Output : [‘Red’, ‘Yellow’, ‘Green’]
1

  • list.remove(elem) :  Searches for the first instance of the given element and removes it (throws ValueError if not found)
colorList = ["Red", "Yellow", "Green"]
print(colorList)
colorList.remove("Yellow")
print(colorList)

Output : [‘Red’, ‘Yellow’, ‘Green’]
[‘Red’, ‘Green’]

  • list.sort() : Sorts the list in place (doesn’t return it).
list1 = [3, 2, 1, 5, 7, 6]
print(list1)
list1.sort()
print(list1)

Output : [3, 2, 1, 5, 7, 6]
[1, 2, 3, 5, 6, 7]

  • list.reverse() : Reverses the list in place (does not return it)
colorList = ["Red", "Yellow", "Green"]
print(colorList)
colorList.reverse()
print(colorList)

Output : [‘Red’, ‘Yellow’, ‘Green’]
[‘Green’, ‘Yellow’, ‘Red’]

  • list.pop(index) : Removes and returns the element at the given index. Returns the rightmost element if index is omitted.
colorList = ["Red", "Yellow", "Green"]
print(colorList)
print(colorList.pop(2))
print(colorList)

Output : [‘Red’, ‘Yellow’, ‘Green’]
Green
[‘Red’, ‘Yellow’]

Python Lists can be either Homogeneous or Heterogeneous.

  • Heterogeneous Data Structures are those data structures that contain a variety of dissimilar types of data.
  • Homogeneous is a set of variables (data) that are one type.
Example :

Here is an example of a list containing Heterogeneous date type – Strings and number(integer)

mixedList = [1, 2.1, "three", 4]
print(mixedList)

Output : [1, 2.1, ‘three’, 4]

Bonus: Choosing the right data type or collection is very important in programming, it increases in efficiency and security.

Do comment if you have any doubt and suggestion on this tutorial.

Note: This All example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python Lists function examples are in Python 3, so it may not same from python 2 or upgraded versions.

Leave a Reply

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