'Accesing 2 items in a list and store it as the variable first_two
I want to access the first two items in the list and change it to the variable "first_two" but i can't find my mistake, please help.
Color_list = ["red", "blue", "green"]
Color_list[0:2]
['red', 'blue']
first_two = "red", "blue"
Color_list[0:2] = ("first_two")
Then it throws out like this
['f', 'i', 'r', 's', 't', '_', 't', 'w', 'o', 'green']
Solution 1:[1]
You can remove " around the variable name. What are you doing now is replacing first two items in Color_list with characters "f", "i", "r", "s", ...
Try:
Color_list = ["red", "blue", "green"]
first_two = "xxx", "yyy"
Color_list[0:2] = first_two # <-- remove "
print(Color_list)
Prints:
['xxx', 'yyy', 'green']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
