Created a list flowers
>>> flowers = ['rose','bougainvillea','yucca','marigold','daylilly','lilly of the valley']
Then,
I had to assign to list thorny
the sublist of list flowers
consisting of the first three objects in the list.
This is what I tried:
>>> thorny = []
>>> thorny = flowers[1-3]
>>> thorny
'daylilly'
>>> thorny = flowers[0-2]
>>> thorny
'daylilly'
>>> flowers[0,1,2]
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
flowers[0,1,2]
TypeError: list indices must be integers, not tuple
>>> thorny = [flowers[0] + ' ,' + flowers[1] + ' ,' + flowers[2]]
>>> thorny
['rose ,bougainvillea ,yucca']
How can I get just the first 3 objects of list flowers, while maintaining the look of a list inside a list?
Slicing notation is [:3]
not [0-3]
:
In [1]: flowers = ['rose','bougainvillea','yucca','marigold','daylilly','lilly of the valley']
In [2]: thorny=flowers[:3]
In [3]: thorny
Out[3]: ['rose', 'bougainvillea', 'yucca']