Python: Split a list into different variables
Split List into Variables
Write a Python program to split a list into different variables.
Sample Solution:
Python Code:
# Define a list 'color' containing tuples, where each tuple represents a color with its name, hexadecimal code, and RGB value
color = [
("Black", "#000000", "rgb(0, 0, 0)"),
("Red", "#FF0000", "rgb(255, 0, 0)"),
("Yellow", "#FFFF00", "rgb(255, 255, 0)")
]
# Unpack the elements of the 'color' list into separate variables 'var1,' 'var2,' and 'var3'
var1, var2, var3 = color
# Print the first color tuple
print(var1)
# Print the second color tuple
print(var2)
# Print the third color tuple
print(var3)
Sample Output:
('Black', '#000000', 'rgb(0, 0, 0)') ('Red', '#FF0000', 'rgb(255, 0, 0)') ('Yellow', '#FFFF00', 'rgb(255, 255, 0)')
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to unpack the first three elements of a list into separate variables and store the rest in another list.
- Write a Python program to split a list of tuples into separate lists.
- Write a Python program to extract specific indices from a list and store them in different variables.
- Write a Python program to split a list into variables while handling cases where the list has fewer or more elements than expected.
Python Code Editor:
Previous: Write a Python program to find missing and additional values in two lists.
Next: Write a Python program to generate groups of five consecutive numbers in a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics