Python – Loop Tuples

Loop Through a Tuple

You can loop through the tuple items by using a for loop.

Example

Iterate through the items and print the values:

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)

Continue reading Python – Loop Tuples

Python – Unpack Tuples

Unpacking a Tuple

When we create a tuple, we normally assign values to it. This is called “packing” a tuple:

Example

Packing a tuple:

fruits = ("apple", "banana", "cherry")

But, in Python, we are also allowed to extract the values back into variables. This is called “unpacking”: Continue reading Python – Unpack Tuples

Python – Update Tuples

Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.

But there are some workarounds.

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple. Continue reading Python – Update Tuples

Python – Access Tuple Items

Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets:

Example

Print the second item in the tuple:

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

Note: The first item has index 0.

Continue reading Python – Access Tuple Items

Python Tuples

mytuple = ("apple", "banana", "cherry")

Tuple

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets. Continue reading Python Tuples

Python – List Methods

List Methods

Python has a set of built-in methods that you can use on lists. Continue reading Python – List Methods

Python – Join Lists

Join Two Lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways are by using the + operator.

Example

Join two list:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

Another way to join two lists is by appending all the items from list2 into list1, one by one: Continue reading Python – Join Lists

Python – Copy Lists

Copy a List

You cannot copy a list simply by typing list2 =
list1
, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.


Use the copy() method

You can use the built-in List method copy() to copy a list. Continue reading Python – Copy Lists

Python – Sort Lists

Sort List Alphanumerically

List objects have a
sort()
method that will sort the list alphanumerically, ascending, by default:

Example

Sort the list alphabetically:

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

Example

Sort the list numerically:

thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)

Sort Descending

To sort descending, use the keyword argument reverse = True: Continue reading Python – Sort Lists

Python – List Comprehension

List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Example:

Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name.

Without list comprehension you will have to write a for statement with a conditional test inside: Continue reading Python – List Comprehension