Tuples are an important data structure in Python which are quite similar to . The main difference between tuples and lists is tuples be modified. Once it's created, it is fixed and unchangeable. Tuples are faster than lists, so if you know your data won't change, it's the correct way to go. Tuples are often used for iterating through a list of fixed items we know won't change. Python lists cannot In this guide, we'll cover how to use Tuples, as well as some of the methods and things you'd want to do with them. To start, let's define a new Tuple: myTuple = ("my", "new", "tuple") print(myTuple) # ('my', 'new', 'tuple') You may also see tuples being defined without brackets. A list of comma-separated values also automatically becomes a tuple: myTuple = "my", "new", "tuple" print(myTuple) # ('my', 'new', 'tuple') Trying to change a tuple, will result in an error: myTuple = ("my", "new", "tuple") myTuple[0] = "your" # TypeError: 'tuple' object does not support item assignment As you might expect, though, that means we can access tuple data using the syntax , to refer to the item at index 0. myTuple[0] Tuples may contain duplicates, so they aren't limited by uniqueness like : Python sets myTuple = ("my", "new", "tuple", "tuple") print(myTuple) # ('my', 'new', 'tuple', 'tuple' ) Finally, tuples can also be nested, just like lists: myTuple = ( ("nested", "tuple"), "my", "new", "tuple", "tuple") Checking Membership using Tuples As with Python sets, we can test for membership using tuples using the and keywords. For example, below, we check if is in our tuple of : in not in apple fruits fruits = ("apple", "pear", "strawberry") print("apple" in fruits) # True print("apple" not in fruits) # False Combining Tuples Since we can't modify tuples if we want to make an updated version of a tuple we need to combine it. That can be done by adding them together: tupleOne = ("one", "two") tupleTwo = ("three", "four") tupleThree = tupleOne + tupleTwo print(tupleThree) # ("one", "two", "three", "four") Sorting a Tuple Since tuples are ordered like Python lists, we can also sort our tuples. However, a tuple has no method , so we have to use the function. Why can't we use a method? Since tuples are immutable! So we have to define a new tuple using : sort() sorted() sort() sorted() myTuple = ("a", "c", "e", "b", "f", "d", "g", "z", "w", "x") myNumberTuple = (1, 3, 5, 2, 7, 4, 6) newTuple = sorted(myTuple) newNumberTuple = sorted(myNumberTuple) print(newTuple) # ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'w', 'x', 'z') print(newNumberTuple) # (1, 2, 3, 4, 5, 6, 7) As with , if you try to sort with multiple types - like integers and strings, you'll end up getting an error. Here we have to use the additional arguments of to sort our list: list.sort() sorted() gives us a number that will be used to compare the list content key if set to true will reverse the order. reverse For example, to put all values which are at the start of a tuple, we could try something like this: b def isB(letter): if(letter == "b"): return 1 else: return 0 myTuple = ("b", "c", "b", 5, "f", "b", 2, "z", "a", "x") newTuple = sorted(myTuple, key=isB, reverse=True) print(newTuple) # ['b', 'b', 'b', 'c', 5, 'f', 2, 'z', 'a', 'x'] Here we define a function that is used in to pass each item in the tuple to the function. If the item in the tuple is , then the order for that item is set to , otherwise it's . This lets us order our list based on conditions other than alphanumeric order. isB sorted() b 1 0 Conclusion Thanks for reading. You can learn more about Python data collections below: Python Data Collections Python Data Collections: Lists Python Data Collections: Tuples Python Data Collections: Sets Python Data Collections: Dictionaries Also published . here