06. Understanding Python Lists

What is a List?

A list in Python is a collection of items enclosed in square brackets [], separated by commas. Lists can store items of different data types, including strings, integers, floats, and even other lists. Lists are ordered, mutable (i.e., changeable), and allow duplicate values.

Creating a List

To create a list, simply enclose the items in square brackets and separate them with commas. Here’s an example:

					channel = ['red', 'green', 'blue', 'alpha']


Accessing List Items

You can access list items by their index. The index of the first item is 0, the second item is 1, and so on. Negative indices can be used to access items from the end of the list.

# Result: red

# Result: alpha


Modifying List Items

Lists are mutable, meaning you can change the value of an item by accessing its index and assigning a new value.

					channel[1] = 'yellow'

# Result: ['red', 'yellow', 'blue', 'alpha']


Adding Items to a List

You can add items to a list using the append() method to add a single item or the extend() method to add multiple items.


# Result: ['red', 'yellow', 'blue', 'alpha', 'cyan']

channel.extend(['magenta', 'white'])

# Result: ['red', 'yellow', 'blue', 'alpha', 'cyan', 'magenta', 'white']


Inserting Items into a List

The insert() method allows you to insert an item at a specific index.

					channel.insert(1, 'orange')

# Result: ['red', 'orange', 'yellow', 'blue', 'alpha', 'cyan', 'magenta', 'white']


Removing Items from a List

You can remove items from a list using the remove() method to remove a specific item, the pop() method to remove an item at a specific index (or the last item if no index is specified), or the del keyword to delete an item at a specific index or the entire list.


# Result: ['red', 'orange', 'blue', 'alpha', 'cyan', 'magenta', 'white']

popped_item = channel.pop(2)
# Result: blue


# Result: ['red', 'orange', 'alpha', 'cyan', 'magenta', 'white']

del channel[1]

# Result: ['red', 'alpha', 'cyan', 'magenta', 'white']


Common List Methods

Python lists come with a variety of built-in methods that make it easy to manipulate and process list items. Here are some of the most commonly used list methods:

  • append(item): Adds item to the end of the list.
  • extend(iterable): Extends the list by appending all items from the iterable.
  • insert(index, item): Inserts item at the specified index.
  • remove(item): Removes the first occurrence of item.
  • pop([index]): Removes and returns the item at index (or the last item if index is not specified).
  • clear(): Removes all items from the list.
  • index(item): Returns the index of the first occurrence of item.
  • count(item): Returns the number of occurrences of item.
  • sort(): Sorts the list in ascending order.
  • reverse(): Reverses the order of the list.
  • copy(): Returns a shallow copy of the list.

learn more about list:  w3schools

Practical Example in Nuke Python

Let’s apply what we’ve learned about lists to a practical example in Nuke Python. Suppose we want to create a custom function that processes the channel list and adds nodes to the Nuke script based on the channels. Additionally, we’ll arrange the nodes horizontally with a space of 200 units between them and ensure they are not connected.

def add_channels(channel_list):
    x_position = 0
    for chan in channel_list:
        node = nuke.createNode('Shuffle', inpanel=False)
        node.setInput(0, None)
        node.setXYpos(x_position, 0)
        x_position += 200

channel = ['red', 'green', 'blue', 'alpha']


Defining the Function add_channels(): This function takes a list of channels as input. It initializes a variable x_position to 0, which will be used to position the nodes horizontally.

Looping Through the Channels: The function iterates through the channel_list. For each channel:

  • A Shuffle node is created using nuke.createNode('Shuffle').
  • The in knob of the node is set to the current channel.
  • The label knob is also set to the current channel to label the node appropriately.
  • The node is disconnected from any input by setting its input to None with node.setInput(0, None).
  • The node’s position is set horizontally based on the x_position variable, ensuring a space of 200 units between each node.
  • The x_position variable is then incremented by 200 units for the next node.

Feel free to explore more about lists and practice with different operations to get comfortable with this fundamental Python data structure. Happy coding!

join my 60-day Nuke Python challenge!

Subscribe now!

Leave a Comment

Your email address will not be published. Required fields are marked *