Lists in Python

Simple lists in Python.

Storing data

We can store mixed data types into the same list:

myList = ["yo", 22, False]

Calling data from the list

Then call them using the […] selector:

print myList[1]

Will output


Calling type from list item

We can also confirm the data type by printing its type:

print type(myList[1])

Results in:

<type 'int'>

Call another variable from within a list

Lists can also contain other variables, for example lets create another variable called ‘foo’ and assign it the Boolean value ‘True’:

foo = True
myList = ["yo", 22, False, foo]
print myList


["yo", 22, False, True]

Notice that the output contains the boolean assigned to ‘foo’, and not the string ‘foo’.

Appending a List with a new item

We can add an additional item to the end of a list using the .append function:

foo = True
myList = ["yo", 22, False, foo]
print myList

Outputs: [“yo”, 22, False, True, ‘goodbye’]

Re-assigning values in a list

We can also reassign a list value:

foo = True
myList = ["yo", 22, False, foo]
myList[0] = "spam"
print myList

Outputs: [‘spam’, 22, False, True, ‘goodbye’]