You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Create a list containing the values a,b,c,d,e, but assign it to a new variable called `x`.
255
-
256
-
```{python,echo=toMessage}
257
-
x = ["a","b","c","d","e"]
258
-
x
259
-
```
260
-
261
-
- Create a list containing the values a,b,c, repeated 5 times and assign it to a new variable
262
-
263
-
```{python,echo=toMessage}
264
-
y = ["a","b","c"] * 5
265
-
y
266
-
```
267
-
268
-
- Create a sublist with indexing. Use the variable you just created and extract the the 10th through to 15th position.
269
-
270
-
```{python, echo=toMessage}
271
-
y[9:14]
272
-
```
273
-
274
-
- With the same variable, let's use "-" indexing to get the last value of our list.
275
-
276
-
```{python, echo=toMessage}
277
-
278
-
y[-1]
279
-
280
-
281
-
```
282
-
283
-
- Now print the values from [0:-1]
284
-
285
-
```{python, echo=toMessage}
286
-
287
-
y[0:-1]
288
-
289
-
290
-
```
291
-
292
-
- Notice how the last value of your list is missing, even though you access it with [-1]. That's because slicing is exclusive, while indexing is inclusive.
293
-
294
-
295
-
- Append the list in variable x. Add the string z.
296
-
```{python, echo=toMessage}
297
-
x.append('z')
298
-
x
299
-
```
300
-
301
-
- Delete the 2nd through 6th entry of x.
302
-
```{python, echo=toMessage}
303
-
del(x[1:5])
304
-
x
305
-
```
306
-
307
-
- Lets go back to variable y. Sort it in reverse order.
308
-
```{python, echo=toMessage}
309
-
y.sort(reverse=True)
310
-
y
311
-
```
312
-
313
-
314
-
- Multiply the y variable by 2.
315
-
316
-
```{python, echo=toMessage}
317
-
318
-
y * 2
319
-
320
-
321
-
322
-
```
323
-
324
-
325
-
- Use the function .remove() to eliminate "a" from your list.
326
-
327
-
```{python, echo=toMessage}
328
-
329
-
y.remove("a")
330
-
331
-
print(y)
332
-
333
-
```
334
-
335
-
- Notice that .remove() eliminates only the first instance of your specified entry.
336
-
337
-
- Create a list of lists. Use the values 1,2,3,4,5, along with the lists x and y.
338
-
339
-
```{python, echo=toMessage}
340
-
nested_list = [[1,2,3,4,5], x, y]
341
-
nested_list
342
-
```
343
-
344
-
- Take your previous list of lists and use indexing to find the third entry of your third list.
0 commit comments