Skip to content

Effortlessly Using Python Tuple Inside List

[

Python Tuple Inside List

In this tutorial, we will explore the concept of Python tuples inside lists. We will cover how to define and use tuples, as well as tuple assignment, packing, and unpacking. By the end of this tutorial, you should have a good understanding of how to work with tuples inside lists in Python.

Table of Contents

  • Python Lists
    • Lists Are Ordered
    • Lists Can Contain Arbitrary Objects
    • List Elements Can Be Accessed by Index
    • Lists Can Be Nested
    • Lists Are Mutable
    • Lists Are Dynamic
  • Python Tuples
    • Defining and Using Tuples
    • Tuple Assignment, Packing, and Unpacking
  • Conclusion

Python Lists

A list in Python is a collection of arbitrary objects. It is similar to an array in other programming languages but more flexible. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets []. For example:

a = ['foo', 'bar', 'baz', 'qux']
print(a) # Output: ['foo', 'bar', 'baz', 'qux']

The important characteristics of Python lists are:

  • Lists are ordered.
  • Lists can contain any arbitrary objects.
  • List elements can be accessed by index.
  • Lists can be nested to arbitrary depth.
  • Lists are mutable.
  • Lists are dynamic.

Lists Are Ordered

A list is an ordered collection of objects. The order in which you specify the elements when defining a list is maintained for the lifetime of that list. Lists with the same elements in a different order are not considered equal:

a = ['foo', 'bar', 'baz', 'qux']
b = ['baz', 'qux', 'bar', 'foo']
print(a == b) # Output: False
print(a is b) # Output: False

Lists Can Contain Arbitrary Objects

A list can contain any assortment of objects. The elements of a list can be of the same type or of varying types:

a = [2, 4, 6, 8]
print(a) # Output: [2, 4, 6, 8]
a = [21.42, 'foobar', 3, 4, 'bark', False, 3.14159]
print(a) # Output: [21.42, 'foobar', 3, 4, 'bark', False, 3.14159]

Lists can even contain complex objects, such as functions, classes, and modules:

import math
a = [int, len, math]
print(a) # Output: [<class 'int'>, <built-in function len>, <module 'math' (built-in)>]

A list can also have any number of objects, from zero to as many as your computer’s memory allows:

a = [] # An empty list
print(a) # Output: []
a = ['foo']
print(a) # Output: ['foo']
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...]
print(a) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...]

Python Tuples

A tuple is similar to a list but is considered an immutable sequence. In other words, once a tuple is defined, it cannot be changed. Tuples are defined by enclosing a comma-separated sequence of objects in parentheses (). Here is an example:

t = ('apple', 'banana', 'cherry')
print(t) # Output: ('apple', 'banana', 'cherry')

Defining and Using Tuples

Tuples can be defined with or without the parentheses, as long as the comma between elements is present. However, it’s recommended to use parentheses for clarity. Tuples can contain elements of any type, just like lists:

t = ('apple', 42, False)
print(t) # Output: ('apple', 42, False)

Tuples can also be nested inside a list:

nested_tup = [(1, 2), (3, 4), (5, 6)]
print(nested_tup) # Output: [(1, 2), (3, 4), (5, 6)]

Tuple Assignment, Packing, and Unpacking

Tuple assignment allows you to assign multiple values simultaneously using a tuple. This can be useful for swapping variables:

a = 10
b = 20
print(a, b) # Output: 10 20
a, b = b, a
print(a, b) # Output: 20 10

When packing, you assign multiple values to a tuple:

t = 1, 2, 3
print(t) # Output: (1, 2, 3)

Unpacking allows you to assign values from a tuple to separate variables:

t = 1, 2, 3
a, b, c = t
print(a, b, c) # Output: 1 2 3

Conclusion

In this tutorial, we covered the characteristics of Python lists and explored tuples inside lists. We learned that lists are ordered, can contain arbitrary objects, and can be accessed by index. Tuples, on the other hand, are immutable and defined using parentheses. We also discussed tuple assignment, packing, and unpacking. Armed with this knowledge, you are ready to use tuples inside lists in your own Python programs.