Variables

Lists in Python

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


Syntax
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

Notes

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.


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

print (cities[0]) #prints Toronto

See Also
Documentation

Data Structures - Python Reference

Add to Slack

< Variable Declaration   |   List Comprehensions >

© 2018 SyntaxDB. All Rights Reserved.