Lists: Difference between revisions

From Computer Science Wiki
Line 8: Line 8:
[[File:Element-slicling.jpg|350px]]
[[File:Element-slicling.jpg|350px]]


Lists, like arrays are indexed starting at zero. The first element in a list is the "zero-th" element. In the example above (thank you for permission to use this image, programiz.com) <ref>https://www.programiz.com/python-programming/list</ref> we can see also negative numbers, which are helpful if you want to select (or slice) a range of elements from a list.
Lists, like arrays, are indexed starting at zero. The first element in a list is the "zero-th" element. In the example above (thank you for permission to use this image, programiz.com) <ref>https://www.programiz.com/python-programming/list</ref> we can see also negative numbers, which are helpful if you want to select (or slice) a range of elements from a list.


The first element is in position zero, the second element is position one, and so on...
The first element is in position zero, the second element is position one, and so on...

Revision as of 11:37, 20 June 2019

Programming basics[1]

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].[2]

Please be careful as lists can be thought of as analogous to arrays, but they aren't the same thing. Arrays traditionally have a fixed memory size whilst lists have dynamic memory allocation. When you are working in Python, call a list a list. When you are working in PHP or Javascript, call an array an array. When you are working in C, call for help!

List Index[edit]

Element-slicling.jpg

Lists, like arrays, are indexed starting at zero. The first element in a list is the "zero-th" element. In the example above (thank you for permission to use this image, programiz.com) [3] we can see also negative numbers, which are helpful if you want to select (or slice) a range of elements from a list.

The first element is in position zero, the second element is position one, and so on...

Creating a list[edit]

# The code below creates a list named polishAnimals 

polishAnimals = ['Bison', 'Moose', 'Deer', 'Lynx', 'Wolf', 'Beaver', 'Otter']

Accessing a list[edit]

# If you want to print a list (kind of ugly) you can simply:

print(polishAnimals)

# However, it is far more common to slice into a list
# The code below accesses the 2nd item in the list named 'polishAnimals'

print(polishAnimals[2])

# there is a lot more to slicing in Python.

Inserting into a list[edit]

# if we want to add onto a list (append) we could simply use the append method. The code below appends wild board onto the end of our list.

polishAnimals.append('wild boar')

# if we wanted to replace a certain element, we could simply overwrite it. Below we are replacing the 2nd item of our list with a new animal:

polishAnimals[1] = 'Stork'

Deleting an element from a list[edit]

# There are a few different ways to remove an element from a list. The first way is to call the remove method.
# The remove method works by finding the name of an element.

polishAnimals.remove('Moose')

# another way to remove an element from a list is to call the pop method. Pop-ing removes an index and return the value you removed. 
# the line below removes the 2nd element from our list and makes it available for us to use. 

polishAnimals.pop(1)

# we might use the pop method like this:

animalsDeleted = polishAnimals.pop[1]

# we can also use the del function.

del polishAnimals[2]

# to delete an entire list, call the clear method (this is only available in Python 3+

polishAnimals.clear

Standards[edit]

  • Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.


References[edit]