Simple lists in Python

From Computer Science Wiki
This is basic programming knowledge [1]

Introduction[edit]

The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.[2]

Lists are a primitive data type, very, very helpful for storing and changing data.

Example of a function[edit]

 1 # creating a list is very easy. See below. 
 2 
 3 list_that_is_empty = []
 4 list_of_computer_parts = ['mouse', 'keyboard', 'CPU', 'display']
 5 list_of_foods = ['kimchi', 'kielbasa', 'noodles', 'rice', 'pierogi']
 6 list_of_students =['joe','sally','frank']
 7 list_of_rooms = ['214a', '214b','212','211','210','209']
 8 
 9 # here are the available methods available to a list: 
10 
11 # list.append(x)
12 # Add an item to the end of the list; 
13 
14 # list.extend(L)
15 # Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
16 
17 # list.insert(i, x)
18 # Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
19 
20 # list.remove(x)
21 # Remove the first item from the list whose value is x. It is an error if there is no such item.
22 
23 # list.pop([i])
24 # Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
25 
26 # list.index(x)
27 # Return the index in the list of the first item whose value is x. It is an error if there is no such item.
28 
29 # list.count(x)
30 # Return the number of times x appears in the list.
31 
32 # list.sort(cmp=None, key=None, reverse=False)
33 # Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
34 
35 # list.reverse()
36 # Reverse the elements of the list, in place.

This example was swiped from the official Python documentation which you should read. [3]

References[edit]

Give a sequence of brief answers with no explanation.

A unit of abstract mathematical system subject to the laws of arithmetic.