Python List


List is a collection type in Python. It can contain any python object with sequential indexes and without worrying about duplicates.


Python List Properties

  • List is a sequential collection.
  • List may have duplicate values.
  • List is mutable.

List Representation

A list object in Python is denoted by [] and it has values seperated by ,

>>> a = [] # empty list

>>> b = [1, "Hello"]

>>> c = [1, "Hello",]

>>> d = [

    1,

    "Hello"

]

>>> e = [

    1,

    "Hello",

]

>>> b == c == d == e

True

A list can easily be written over multiple lines. The command at the end of the list is optional.


Python List Attributes

List has below built-in functions:

  • append
  • extend
  • copy
  • count
  • clear
  • index
  • insert
  • pop
  • remove
  • reverse
  • sort


Python List Comprehension


List comprehension is a shortcut technique to create a new list object with just one-liner syntax, as below:

[expression for loop if condition]