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.
print(channel[0])
# Result: red
print(channel[-1])
# 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'
print(channel)
# 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.
channel.append('cyan')
print(channel)
# Result: ['red', 'yellow', 'blue', 'alpha', 'cyan']
channel.extend(['magenta', 'white'])
print(channel)
# 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')
print(channel)
# 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.
channel.remove('yellow')
print(channel)
# Result: ['red', 'orange', 'blue', 'alpha', 'cyan', 'magenta', 'white']
popped_item = channel.pop(2)
print(popped_item)
# Result: blue
print(channel)
# Result: ['red', 'orange', 'alpha', 'cyan', 'magenta', 'white']
del channel[1]
print(channel)
# 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): Addsitemto the end of the list.extend(iterable): Extends the list by appending all items from theiterable.insert(index, item): Insertsitemat the specifiedindex.remove(item): Removes the first occurrence ofitem.pop([index]): Removes and returns the item atindex(or the last item ifindexis not specified).clear(): Removes all items from the list.index(item): Returns the index of the first occurrence ofitem.count(item): Returns the number of occurrences ofitem.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['in'].setValue(chan)
node['label'].setValue(chan)
node.setInput(0, None)
node.setXYpos(x_position, 0)
x_position += 200
channel = ['red', 'green', 'blue', 'alpha']
add_channels(channel)
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
Shufflenode is created usingnuke.createNode('Shuffle'). - The
inknob of the node is set to the current channel. - The
labelknob is also set to the current channel to label the node appropriately. - The node is disconnected from any input by setting its input to
Nonewithnode.setInput(0, None). - The node’s position is set horizontally based on the
x_positionvariable, ensuring a space of 200 units between each node. - The
x_positionvariable 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!