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!']