Lists and Dictionaries: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
people = [] # empty list | people = [] # empty list | ||
animals = ['daisy','pat','wabbit','bunny'] | animals = ['daisy','pat','wabbit','bunny'] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 10: | Line 10: | ||
Access elements | Access elements | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
print(animals[0]) # first item | print(animals[0]) # first item | ||
print(animals[-1]) # last item | print(animals[-1]) # last item | ||
print(animals[-2]) # penultimate item | print(animals[-2]) # penultimate item | ||
print(f"{animals[0].title()} is a cow.") # in an f-string | print(f"{animals[0].title()} is a cow.") # in an f-string | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 18: | Line 18: | ||
Modify, add, insert elements | Modify, add, insert elements | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
animals[3] = 'veau' # replace item 3 | animals[3] = 'veau' # replace item 3 | ||
animals.append('croc') # add new item to end of list | animals.append('croc') # add new item to end of list | ||
animals.insert(1,'pato') # insert item at index 1 | animals.insert(1,'pato') # insert item at index 1 | ||
Line 26: | Line 26: | ||
Remove elements | Remove elements | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
del animals[0] # delete item at index 0 | del animals[0] # delete item at index 0 | ||
popped_animal = animals.pop() # remove last item | popped_animal = animals.pop() # remove last item | ||
popped_animal = animals.pop(2) # remove item at index 2 | popped_animal = animals.pop(2) # remove item at index 2 | ||
animals.remove('veau') # remove (first instance of) item by value | animals.remove('veau') # remove (first instance of) item by value | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Organize Lists | Organize Lists | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
animals.sort() # sort values a-z | animals.sort() # sort values a-z | ||
animals.sort(reverse=True) # sort values z-a | animals.sort(reverse=True) # sort values z-a | ||
animals.reverse() # reverse order (not z-a) | animals.reverse() # reverse order (not z-a) | ||
print(sorted(animals)) # temporarily sort | print(sorted(animals)) # temporarily sort | ||
len(animals) # returns number of items | len(animals) # returns number of items | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Loop on a List | |||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
for animal in animals: # for loop requires a colon | |||
print(animal) # indent each line in the loop | |||
print(f"Hello {animal.title()}") | |||
print(f"We have {len(animals)} animals!") # stop indenting after the loop | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 13:37, 31 March 2024
Lists
Create a list
people = [] # empty list
animals = ['daisy','pat','wabbit','bunny']
Access elements
print(animals[0]) # first item
print(animals[-1]) # last item
print(animals[-2]) # penultimate item
print(f"{animals[0].title()} is a cow.") # in an f-string
Modify, add, insert elements
animals[3] = 'veau' # replace item 3
animals.append('croc') # add new item to end of list
animals.insert(1,'pato') # insert item at index 1
Remove elements
del animals[0] # delete item at index 0
popped_animal = animals.pop() # remove last item
popped_animal = animals.pop(2) # remove item at index 2
animals.remove('veau') # remove (first instance of) item by value
Organize Lists
animals.sort() # sort values a-z
animals.sort(reverse=True) # sort values z-a
animals.reverse() # reverse order (not z-a)
print(sorted(animals)) # temporarily sort
len(animals) # returns number of items
Loop on a List
for animal in animals: # for loop requires a colon
print(animal) # indent each line in the loop
print(f"Hello {animal.title()}")
print(f"We have {len(animals)} animals!") # stop indenting after the loop