Lamborghini Huracán LP 610-4 t

Topics

List

List Creation

List is one of the most commonly used data structure in Python. It contains a list of items separated by commas and enclosed within square brackets. A list can be created easily as shown below:


# only numbers
num_list = [3, 6, 2, 10] 

# only string                           
word_list = ['hello', 'python', 'programming']    

# combination of string and numbers 
mixed_list = ['a', 3.5, 6, 'b', 'c']                
						

Accessing Values in a List

Use square bracket to access the value in a list. The first item in a list is accessed with index 0. See examples below:


num_list = [4, 12, 'a', 'b', 5.0]

# value: 4, first item in the list
first_item = num_list[0]           

# value: 5.0, last item in the list
last_item = num_list[4]             

# use -1 to access elements starting from the right
last_item = num_list[-1]            

# value: [4, 12] Note: This is called list slicing
first_two_items = num_list[0:2]    

# value: [12, 'a', 'b']
items_in_middle = num_list[1:-1]    

# error, list is 0-indexed
invalid_index = num_list[5]         
						

Updating a List

You can change the value of existing items, add new item or remove existing item(s) from a list.


>>> a = [1, 2, 3, 4]

# change the 2nd item from 2 to 5
>>> a[1] = 5            
>>> a
[1, 5, 3, 4]

# remove the number 3
>>> a.remove(3)         
>>> a
[1, 5, 4]

# remove last item
>>> a.pop()             
4
>>> a
[1, 5]

# add the number 10 to the end of the list
>>> a.append(10)        
>>> a
[1, 5, 10]
						

Other useful list operations

Python includes a few useful list functions and methods.


>>> x = [2, 6, 3, 1, 10, 5]

# get number of items in the list
>>> len(x)                      
6

# sort the list in ascending order
>>> x.sort()                   
>>> x
[1, 2, 3, 5, 6, 10]

# reverse the order of the elements in the list
>>> x.reverse()                 
>>> x
[10, 6, 5, 3, 2, 1]

# get the maximum value in the list
>>> max(x)                      
10

# get the minimum value in the list
>>> min(x)                      
1

# get the number of 5 in the list
>>> x.count(5)                  
1
						

List Comprehensions

List comprehension offers a concise way of creating a new list from existing sequences.


>>> x= [1, 2, 3, 4, 5 ]

# create a new list with each element multiplied by 3.
>>> y = [ i*3 for i in x ] 
>>> y
[3, 6, 9, 12, 15]

# create a list with even numbers
>>> even = [ i for i in range(1, 10) if i%2==0 ] 
>>> even
[2, 4, 6, 8]
						

Code Snippets

This section will contain some code snippets to show some practical use of the topic that you have learnt.


# Calculating Maximum Value
# --------------------------
numbers = [2, 4, 1, 7, 3]

# Assume 1st number is biggest
biggest = numbers[0]

# Go through each number in the list
for n in numbers:
	
	if n > biggests:
	# if a bigger number is found, assign its value to biggest
		biggest = n

# the answer is 7 for this example
print biggest

# Alternative approach
# Using the max() function

biggest = max(numbers)
print biggest

# Some tricks with list indexing
# -------------------------------
# Reverse items in a list (or string)

>>> numbers = [7, 4, 2, 3]
>>> numbers[::-1] # reversing the items
numbers = [3, 2, 4, 7]

# Checking for palindrome
# a sequence of characters which reads the same backward or forward

x = [1, 3, 1]
isPalindrome = x == x[::-1]
print isPalindrome # output is True

y = [1, 3, 2]
isPalindrome = y == y[::-1]
print isPalindrome # output is False
						
2014 KILLER BEAN