w3resource

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:

Python List: Split a list into different variables

Flowchart:

Flowchart: Split a list into different variables

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/list/python-data-type-list-exercise-43.php