Python List Basic Flashcards
How do you create a list named my_list
with the elements 1, 2, 3
in Python?
```python
my_list = [1, 2, 3]
~~~
How do you access the first element of the list nums = [10, 20, 30]
?
```python
nums[0] # Returns 10
~~~
Given fruits = ["apple", "banana", "cherry"]
, how do you change the value of “banana” to “orange”?
```python
fruits[1] = “orange”
~~~
How do you append the integer 5
to numbers = [1, 2, 3, 4]
?
```python
numbers.append(5)
~~~
Given colors = ["red", "green", "blue"]
, how do you add “yellow” as the second element, using a list method?
```python
colors.insert(1, “yellow”)
~~~
Given letters = ["a", "b", "c", "d"]
, how do you remove “c” from the list by its value?
```python
letters.remove(“c”)
~~~
Given nums = [1, 2, 3]
, how do you remove and return the last element using a list method?
```python
popped_value = nums.pop() # Returns 3
~~~
Given nums = [1, 2, 3, 4]
, how do you remove and return the element at index 1 using a list method?
```python
popped_value = nums.pop(1) # Removes and returns 2
~~~
How do you concatenate list1 = [1, 2]
and list2 = [3, 4]
into [1, 2, 3, 4]
using the +
operator?
```python
combined = list1 + list2
~~~
Given list1 = [1, 2]
and list2 = [3, 4]
, how do you extend list1
so it becomes [1, 2, 3, 4]
?
```python
list1.extend(list2)
~~~
How do you slice the first 3 elements from nums = [10, 11, 12, 13, 14]
?
```python
slice_part = nums[:3] # [10, 11, 12]
~~~
How do you slice nums = [10, 20, 30, 40, 50]
to get [30, 40]
using Python slice notation?
```python
slice_part = nums[2:4] # [30, 40]
~~~
How do you get every second element from nums = [1, 2, 3, 4, 5, 6]
using slicing?
```python
every_second = nums[::2] # [1, 3, 5]
~~~
Given my_list = [2, 4, 6, 8]
, how do you reverse the list in-place (so that my_list
becomes [8, 6, 4, 2]
)?
```python
my_list.reverse()
~~~
How do you reverse a list nums = [1, 2, 3]
without modifying the original list (using slicing)?
```python
reversed_list = nums[::-1] # [3, 2, 1]
~~~
Given numbers = [4, 2, 6, 1]
, how do you sort it in ascending order in-place?
```python
numbers.sort()
~~~
How do you sort numbers = [4, 2, 6, 1]
in descending order in-place?
```python
numbers.sort(reverse=True)
~~~
How do you create a sorted copy of nums = [3, 1, 2]
without modifying nums
itself?
```python
sorted_copy = sorted(nums)
~~~
Given nums = [1, 2, 3]
, how do you double every value and return a new list using a list comprehension?
```python
doubled = [x * 2 for x in nums] # [2, 4, 6]
~~~
How do you filter out odd numbers and keep only even numbers from nums = [1, 2, 3, 4, 5, 6]
using a list comprehension?
```python
evens = [x for x in nums if x % 2 == 0] # [2, 4, 6]
~~~
Given words = ["apple", "banana", "cherry"]
, how do you create a list of their lengths using a list comprehension?
```python
lengths = [len(word) for word in words] # [5, 6, 6]
~~~
How do you check if the list my_list = [2, 4, 6]
is empty or not, in a Pythonic way?
```python
if not my_list:
print(“Empty list”)
else:
print(“Not empty”)
~~~
Given nums = [5, 3, 1, 4, 2]
, how do you find the smallest value using a built-in function?
```python
minimum = min(nums) # 1
~~~