3.4.2. Lists - JulTob/Python GitHub Wiki
π Declaration
my_list = [β1β, β2β, β3β, β4β, β5β ]
π’ Properties
len(my_list)
π¦ Slicing π₯
my_list[0] # '1'
my_list[1:3] # '2', '3' (3rd = 4, not included)
my_list[ :2] # '1', '2'
my_list[::2] # indices pares: '1', '3', '5'
my_list[start:stop:step]
π― Deindexing
my_list.index('1') # 0
[2, 2, 5].index(2)
# 0
## First indexing for a 2
[2, 2, 5].index(2,1)
# 1
## Indexing for a 2 after 1st
πΏ Stacking π₯π€
my_list.pop() # Eliminates last, 5, and returns the value
my_list.pop(2) # Eliminates at 2, and returns the value
my_list.remove('7') # Eliminates '7'
my_list.append(β5β) # Appends last, 5
my_list.insert(4, '7') # Insert the 4th, '7', and pushes back
my_list.insert(3, 3.14) # [β1β,β2β, β3β, 3.14 ,β4β, β5β ]
my_list.extend([β6β,β7β,β8β])
my_list[0:2]=[β0β,β1β,β2β] # [β0β,1β, β2β, 3.14 ,β4β, β5β,β6β,β7β,β8β ]
my_list.remove(β1β)
len(my_list) #Length
print(my_list)
print(*my_list)
my_list.clear() # Cleans the list of all elements
del my_list[4] # Deletes 4th
del my_list # Deletes list
π¦ Sorting
my_list.sort() # Sorts
sorted(my_list) # Another list, sorted
my_list.sort(*,key=None, reverse = False)
my_list.reverse() # Reverses
srt_nums = sorted(nums) # creates new list
nums.sort() # Alters nums
π Belonging
for item in my_list: # items is a variable that only exist in the for
print(βNumber β + item)
if item in my_list:
print("Is inside")
[βnumber β+ item for item in my_list]
>>> [βnumber 1β,βnumber 2β, βnumber 3β, βnumber 4β, βnumber 5β ]
π¦ Searching
list.count(x)
# Return the number of times x appears in the list.
π¦ Listing
my_integers = [1, 2, 3, 4, 5]
for n in my_integer:
print(n)
𦩠Joining and concatenation
# List Concatenation
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
# [1, 3, 5, 7, 2, 4, 6, 8]
repeat = even_numbers * 3
# [2,4,6,8,2,4,6,8,2,4,6,8]
π Copy
π So you can use a copy of the list, maybe as an index.
list.copy()
Return a shallow copy of the list. Equivalent to a[:].
# Python's list slice syntax can be used without indices
# for a few fun and useful things:
# You can clear all elements from a list:
>>> lst = [1, 2, 3, 4, 5]
>>> del lst[:]
>>> lst
[]
# You can replace all elements of a list
# without creating a new list object:
>>> a = lst
>>> lst[:] = [7, 8, 9]
>>> lst
[7, 8, 9]
>>> a
[7, 8, 9]
>>> a is lst
True
# You can also create a (shallow) copy of a list:
>>> b = lst[:]
>>> b
[7, 8, 9]
>>> b is lst
False
list.extend(otherList) # Merges together
list.copy()
listcopy = list.copy()
a = [0, 1, 1, 2, 3, 5, 8, 13]
a + a # [0, 1, 1, 2, 3, 5, 8, 13, 0, 1, 1, 2, 3, 5, 8, 13]
a[-1] # 13
b = list(range(3))
# [0,1,2]
b = list(range(3,6))
# [3,4,5]
b = list(range(0,10,2))
# [0,2,4,6,8]
π min max sum
nums = [5, 3, 9]
print( min(nums) )
print( max(nums) )
print( sum(nums) )
Checking
if 3 in nums:
print(3)
if 3 not in nums:
print(0)
# is empty?
nums = []
if not nums: # could also say 'if nums == []'
print("Empty")
# ---- #
# using the while loop to remove a certain value
names = [ "Bob", "Jack", "Rob", "Bob", "Robert" ]
while "Bob" in names:
names.remove("Bob") # removes all instances of 'Bob'
print(names)
π¦ List in loops
# using a for loop to print all items in a list
sports = [ "Baseball", "Hockey", "Football", "Basketball" ]
for sport in sports:
print(sport)
for i in range(len(numbers)):
numbers[i] = numbers[i] * 2
Remove Duplicates
names = ['Bob', 'Kenny', 'Amanda', 'Bob', 'Kenny']
clean_names = []
for name in names:
if name not in clean_names:
clean_names.append(name)
names = sorted(clean_names)
del(clean_names)
print(names)
How to get a list from a range.
list(range(4))
π¨ Stack example
stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
π Queue
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
π‘ Example
kingslayers = [
"Jamie",
"Drogo",
"Little_Finger",
"Tommen"
]
kingslayers.append("Jon")
for ks in kingslayer:
print ("The curse of the Kingslayer got " + kingslayers[ks])
fruits=[]
fruits.append(βAppleβ)
fruits.append(βMangoβ)
fruits.append(βOrangeβ)
print(fruits)
L = [1, 'a', max, 3 + 4j, 'Hello, world!']