Lists in Python

Lists are used to store multiple items sequentially in one variable. Equivalent of an array in other languages. Lists are mutable.

list_name = [] #empty list to start
list_name2 = [item1, item2] #non empty list to start

list_name.append(item) #add to the list

list_name[index] #access the item at the list index


Items inside of the list can be of different types. Data structures and other lists can also be stored within a list.

The array index range is from 0 to size minus 1. Negative indices start at the end of the array and work backwards.

Lists can be added (using addition operator) or concatenated (using the extend function). There are many other list functions, which can be found in the PyDocs.

cities = ["Toronto", "San Francisco", "Seattle"]

print (cities[0]) #prints Toronto

See Also

Data Structures - Python Reference

Add to Slack

< Variable Declaration   |   List Comprehensions >

© 2019 SyntaxDB. All Rights Reserved.