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)
You can loop through the tuple items by using a for
loop.
Iterate through the items and print the values:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
When we create a tuple, we normally assign values to it. This is called “packing” a tuple:
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
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.
But there are some workarounds.
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
You can access tuple items by referring to the index number, inside square brackets:
Print the second item in the tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
Note: The first item has index 0.
mytuple = ("apple", "banana", "cherry")
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 has a set of built-in methods that you can use on lists. Continue reading Python – List Methods
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the +
operator.
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
You cannot copy a list simply by typing list2 =
, because:
list1list2
will only be a reference to list1
, and changes made in list1
will automatically also be made in list2
.
You can use the built-in List method copy()
to copy a list. Continue reading Python – Copy Lists
List objects have a
method that will sort the list alphanumerically, ascending, by default:
sort()
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
Sort the list numerically:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
To sort descending, use the keyword argument reverse = True
: Continue reading Python – Sort Lists
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