Create lists in Python

Create an empty list

>>> myList=[]
>>> myList
[]

Create lists initialized with a given value

>>> myList=[2]*5
>>> myList
[2, 2, 2, 2, 2]

Create and populate lists / arrays

>>> myList=['1', "second", 3.0]
>>> myList
['1', 'second', 3.0]

Create and populate multidimensionnal lists

>>> myList=[[1,2,3],[4,5]]
>>> myList
[[1, 2, 3], [4, 5]]

Create a list with range()

>>> myList=list(range(10))
>>> myList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

See also


Last update : 06/11/2018