Lists

Introduction

Python lists are meant to hold various types of data. It is composed of items inside brackets “[ ]”  which are indexed and can be messed with, adding and removing items. Lists are mutable they can be altered even after their creation.

You can declare a variable list empty:

List1 = []


Or you can initialize it:

 List2 = [ 1, 2, 3, 4, 5, 6, 7, 8, ….]


And you can even place different types of data in the same list:

List3 = [ 1, “Hello”, 4.68, True, “World” ]


Accessing List

Lists are indexed by integers starting on position 0 and until n-1, being n the length of the list.

Example:

 List4 = [ "Hey", "nice", "to", "meet", "you"]


Has length 5 and can be indexed like this:

 

0 1 2 3 4

 

print( List4[0] )
print( List4[1] )
print( List4[2] )
print( List4[3] )
print( List4[4] )

 

Output:

Hey
nice
to
meet
you


Which will print out every member of List4.

But we can also access it with negative numbers which do it from the end to the start. Like this:

 

-5 -4 -3 -2 -1

 

print( List4[-1] )
print( List4[-2] )
print( List4[-3] )
print( List4[-4] )
print( List4[-5] )

 

Output:

you
meet
to
nice
Hey


This will print the List4 backwards.
Or we can also use a for to iterate over the list.

 

int = 0
while int < len(List4):
    print(List4[int])
    int += 1

 

Output:

Hey
nice
to
meet
you


This consists of a while loop that increments a variable until it is out-of-bounds of List4.
Important: Since the list indexes start at 0, then the last index is the size of the list - 1.

Operations

Repetition

We can repeat the contents by x times.

Example:

List5 = List4 * 3
print( List5 )

 

Output:

['Hey', 'nice', 'to', 'meet', 'you', 'Hey', 'nice', 'to', 'meet', 'you', 'Hey', 'nice', 'to', 'meet', 'you']


Will print the List5 which is 3 times the List4. 

 

Concatenation
 

We can also concatenate lists by adding them.

Example:

List6 = ["How’s" , "the", "weather"]
print( List4 + List6 )    

 

Output:

 ['Hey', 'nice', 'to', 'meet', 'you', 'How’s', 'the', 'weather']


Will print a bigger list containing List4 and List6    

 
Membership

There is a way to finding out if a specific value exists in a specific list.
 
Example:

print( "Hey" in List4)

 

Output:

True


This will print out True because the string “Hey” indeed exists in List4.

 

Iteration
 

Using the Membership operator, we can iterate over every member of a list.

Like this:

for member in List4:
    print(member)

 

Output:

Hey
nice
to
meet
you


For each iteration of this loop, the temporary variable member will be assigned a value of the list being printed out and passing on to the next value.

Length

For last there is a way to get the size of a specific list. Using the method len().

Example:

print( len( List4 ) )

 

Output: 5


This code will print out the size of List4 which is 5.

 

Slicing
 

Lists can be sliced to show only desired values or to iterate over desired indexes.

print(List4[1:3])
print(List4[2:])


First-line will print from index 1 to 3 and the second will print starting on index 2.

Working with Lists

Lists can be very useful to hold data, like student names for a runtime, trying to compare students or sort the list alphabetically. Since lists are very versatile on what data can be held, it eases a lot its use.

Function and Methods

Python List Built-in functions

 

Function

Description

E.g

len(list)

It is used to calculate the length of the list.

len( List7 ) will return 3

max(list)

It returns the maximum element of the list.

max( list7 ) will return 8

min(list)

It returns the minimum element of the list.

min( list7 ) will return 1

list(seq)

It converts any tuple to a list.

list((1,2,3,4,5,6,7)) will create the list

[ 1, 2 , 3, 4, 5, 6, 7]

 

Python List built-in methods

 

Function

Description

E.g

list.append(obj)

The object obj is added to the list.

List9 = ["Hello", "World"]

List9.append("Smile")

Will add the string “Smile” to List9

list.clear()

It removes all the elements.

List9.clear() clears the entire list

List9 is now [] (an empty list)

List.copy()

It returns a shallow copy of the list.

List10 = List9.copy()

List10 will be a copy of List9.

list.count(obj)

It returns the number of occurrences of the specified object in the list.

print(List9.count("Hello"))

It will print out 1 which is the number of times the string “Hello” is in List9.

list.extend(seq)

The sequence represented by the object seq is extended to the list.

List9.extend("Hey", "Smile")

It extends List9 adding those two elements.

list.index(obj)

It returns the lowest index in the list that object appears.

print(List9.index("Hello"))

It prints out 0 which is the index of “Hello”.

list.insert(index, obj)

The object is inserted into the list at the specified index.

List9.insert(1, "Hey")

Inserts “Hey” right after “Hello” pushing “World” to the right.

list.pop(obj=list[-1])

It removes and returns the last object of the list.

print(List9.pop())

Will remove the string "World" and print it.

list.remove(obj)

It removes the specified object.

List9.remove("World")

Removes the string “World” from List9.

list.reverse()

It reverses the list.

List9.reverse()

Reverses the order of List9 to ["World”, “Hello”]

list.sort([func])

It sorts the list by using the specified compare function if given.

List11 = [ 3, 67, 12, 5]

List11.sort() will return List11 sorted by  default

[ 3, 5, 12, 67] but a sorting function can be fed to the function sort.

 

 

Related Tutorials