Python: Split a list into different variables
Python List: Exercise - 43 with Solution
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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/list/python-data-type-list-exercise-43.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics