Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
92 lines (64 loc) · 1.45 KB

File metadata and controls

executable file
·
92 lines (64 loc) · 1.45 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

list - basics

Examples in Python 3

"""
Simple list of integers
"""
my_list = [1,2,3,4,5]
print(my_list)


"""
Indexing
"""
print(my_list[0])
print(my_list[4])
print(my_list[-1])


"""
Slicing
"""
print(my_list[:2])
print(my_list[3:5])
print(my_list[2:])


"""
Copying lists
"""
# What happenes if we just copy the variable?
my_list = [1,2,3,4,5]
copy = my_list
copy[0] = 42
print("Copy:", copy)     
print("My list:", my_list)  # Both are the same even though we only changed one!

# Check the reference of copy and my_list
print("Copy has the same reference as my_list:", copy is my_list)


# Copy list my using slice
my_list = [1,2,3,4,5]
copy = my_list[:]
copy[0] = "42"
print("Copy:", copy)
print("My list:", my_list)  # Now only copy was changed, the reference was no longer the same

# Check that the reference to copy and my_list are NOT the same
print("Copy has the same reference as my_list:", copy is my_list)


"""
Merge lists
"""
x = [1,2,3]
y = ["apple", "banana"]
x += y
print(x)



"""
Replace and remove
"""
# Replace and add more
x = ["apple", "banana", "cherry", "date"]
x[2:4] = ["blueberry", "blackcurrant", "cocunut", "cranberry", "cantaloupe"]
print(x)

# Remove
x[3:5] = []
print(x)

Reference and read more

Tutorial - Lists

Revision history

2014-07-21 (sylvanas) PA1 First try.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.