SnipBits

Python Lists

Lists are an essential Data Structure that every one may use one day,

They allow us to group together multiple values and reference them all with the same name.

for Example

numbers = [1,2,3]

a list can hold values of different Types:

items = ["hi",2,3,4,"text"]

we can check if an item is contained into a list with the in opertaor

we can reference the items in a list by their index , starting from zero :

items[0] # "hi"
items[1] # 2

using the same notation we can change the value stored at a specific index:

items[0] = "bye"

we can also use the the index() method, this will return to us what is stored at these index .

as with strings, a negative index will start searching from the end

we can get the number of items contained in a list using the len() global function, the same we used to get the length of the string len(items)

adding the Items can be done with append() method, or the extend method like this items.extend(["test"])

you can check here to see how to remove Duplicates from a list in Python


Maher Gallaoui

You like our Content? Follow us to stay up-to-date.