Center-for-Health-Data-Science / PythonTsunami

An introductory course to Python by the Center of Health Data Science (University of Copenhagen)
https://center-for-health-data-science.github.io/PythonTsunami/
MIT License
16 stars 12 forks source link

Questions on lists #5

Open enryH opened 3 years ago

enryH commented 3 years ago

Collection of questions on list

Code-Snippets


## List copies

a = 40
another_list = [6, 4, 1, 2, 5, a]
list2 = list(another_list) #.copy()
another_list.sort()

print(another_list)
print(list2)

## Nested list copies

# create a list
inner_list = [40, 50, 60]

outer_list = [6, 4, 1, 2, 5, inner_list]
copy_outer_list = list(outer_list) #.copy()
# copy_outer_list = outer_list.copy()

print(outer_list)
print(copy_outer_list)

inner_list[2] = 500
outer_list[1] = 100
print(outer_list)
print(copy_outer_list)
enryH commented 3 years ago

Maybe extend the modifying tricks section

numbers = [1, 2, 3, 4, 5]
numbers[1] = ['a','b','c'] 
print(numbers) 

numbers = [1, 2, 3, 4, 5]
numbers[1:2] = ['a','b','c'] 
print(numbers) 

numbers = [1, 2, 3, 4, 5]
numbers[1:3] = ['a','b','c'] 
print(numbers) 

numbers = [1, 2, 3, 4, 5]
numbers[1:4] = ['a','b','c'] 
print(numbers) 
enryH commented 3 years ago

In the current order, list comprehensions are introduced before loops.