Python Basics: Tuples and Lists

1 post Page 1 of 1
Contributors
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Python Basics: Tuples and Lists
comathi
As we saw in the previous tutorial, Python data types also include Tuples and Lists. We'll see more about these in this tutorial.

Tuples

First and foremost, we must define what a Tuple is. A Python tuple is an ordered collection of object references. Objects referenced by a tuple do not have to be of the same type (for instance, you may have Numbers and Strings in the same Tuple). Like a String, it is immutable, which means it cannot be altered once created.

Creating Tuples

Python Tuples are simply created by assigning a comma-seperated list of objects to a variable, like so:
Code: Select all
my_tuple = "a string", 11, 42.3, "a number... no it isn't"
Tuples may also contain other tuples, for instance:
Code: Select all
my_tuple = "a string", ("a string in another tuple", 11), 42.3
Because Tuples are a sequence, they can be sliced, exactly like Strings:
Code: Select all
my_tuple = "a string", 11, 42.3, "a number... no it isn't"

print(my_tuple[1]) #Prints 11
They do however offer two aditionnal functions: count() and index(). The first returns the number of time a certain object occurs in the tuple, while the second return the index of the specified object in the tuple.

Lists
Like Tuples and Strings, Lists are also a collection type object. They are ordered, like Tuples, but unlike Tuples and Strings, they are mutable. This means that modifying a List in a method, for example, affects the actual List:
Code: Select all
my_list = [1, "s", 15.54]

def change_item(l,i):
>>>>l[i] = 42

change_item[l,0]

print(my_list) #Prints [42, "s", 15.54]
Creating Lists
List creating in Python is similar to JavaScript. That is, a variable name, followed by square brackets, and then the contents of the List:
Code: Select all
l = ["item1", 2, 3.33]
Like Tuples, Lists can contain more than one type of object, including other Lists
Code: Select all
l = ["item1", ["item of another List", 4.56], 2, 3]
Because Lists are sequences, they can also be sliced like Strings or Tuples:
Code: Select all
l = ["item1", 2, 3.33]

print(l[1]) #Prints out 2
Splitting Strings into Lists
Like in VB.NET, Strings can be split into Lists by specifing a String and a delimiter:
Code: Select all
my_string = "C:\Users\Craig\Documents\JustinBieber\Pics"
my_split_string = my_string.split("\")

print(my_split_string) #Prints out ["C", "Users", "Craig", "Documents", "JustinBieber", "Pics"]
More List functions
Here are some interesting functions that Python Lists offer:

l.append(x): Appends x to the List l
l.count(x): Like with Tuples, counts the number of times x is found in the List
l.index(x): Like with Tuples, returns the index of the first instance of x in the List
l.insert(i,x): Inserts object x at index i of the List
l.pop(i): Removes the List item at index i

Lots more functions are available, but since I can't show them all here, I suggest you check out Python's official documentation on Lists: Python List documentation

List comprehension
One of the many neat features of Python is list comprehension. This allows you to create Lists using loops and conditionnals, like so:
Code: Select all
my_list = [c for c in range(50) if x % 2 == 0]
There might be a few things you don't understand in the above code, but basically, it creates a list populated with all the numbers from 0 to 50 whose modulo with 2 is 0 (which means they are evenly divisible by 2).

That's all the important stuff to know about Tuples and Lists in Python. Hopefully, this tutorial has been of use to you, and as always, go crazy! cooll;
1 post Page 1 of 1
Return to “Tutorials”