Lists are an essential Python data structure.
The allow you to group together multiple values and reference them all with a common name.
dogs = ["Roger", "Syd"]
A list can hold values of different types:
items = ["Roger", 1, "Syd", True]
You can check if an item is contained into a list with the
print("Roger" in items) # True
A list can also be defined as empty:
items = 
You can reference the items in a list by their index, starting from zero:
items # "Roger" items # 1 items # True
Using the same notation you can change the value stored at a specific index:
items = "Roger"
You can also use the
items.index("Roger") # 0 items.index("Syd") # 2
As with strings, using a negative index will start searching from the end:
items[-1] # True
You can also extract a part of a list, using slices:
items[0:2] # ["Roger", 1] items[2:] # ["Syd", True]
Get the number of items contained in a list using the
len() global function, the same we used to get the length of a string:
You can add items to the list by using a list
or the extend() method:
You can also use the
items += ["Test"] # items is ['Roger', 1, 'Syd', True, 'Test']
+=don’t forget the square brackets. Don’t do
items += "Test"or
items.extend("Test")or Python will add 4 individual characters to the list, resulting in
['Roger', 1, 'Syd', True, 'T', 'e', 's', 't']
Remove an item using the
You can add multiple elements using
items += ["Test1", "Test2"] #or items.extend(["Test1", "Test2"])
These append the item to the end of the list.
To add an item in the middle of a list, at a specific index, use the
items.insert(1, "Test") # add "Test" at index 1
To add multiple items at a specific index, you need to use slices:
items[1:1] = ["Test1", "Test2"]
Sort a list using the
Tip: sort() will only work if the list holds values that can be compared. Strings and integers for example can’t be compared, and you’ll get an error like
TypeError: '<' not supported between instances of 'int' and 'str'if you try.
sort() methods orders uppercase letters first, then lowercased letters. To fix this, use:
Sorting modifies the original list content. To avoid that, you can copy the list content using
itemscopy = items[:]
or use the
sorted() global function:
that will return a new list, sorted, instead of modifying the original list.
One more thing! ⚠️ ✋
It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.
It's not "just a course". It's a big event I organize once a year.
At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.
Because once you got the fundamentals, you only learn by working on real, exciting projects.
To find out more, visit bootcamp.dev