Python Lists Tutorial and Examples

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 store data in a bracket with comma separated. It’s one of the most frequently used and very versatile datatypes 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 start from 0. Here is an example, how to create a list in python and print in the console.

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

[‘School’, ‘MMKO’, 2000]

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

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.

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

  • list.insert(index, elem) – inserts the element at the given index, shifting elements to the right.

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().

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

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)

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

  • list.sort() : Sorts the list in place (doesn’t return it).

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

  • list.reverse() : Reverses the list in place (does not return it)

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.

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 or dissimilar type of data.
  • Homogeneous is a set the variables (data) are one type.
Example :

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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.