Supprimer Rendre public Rendre privé Add tags Delete tags
  Ajouter un tag   Annuler
  Supprimer le tag   Annuler
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Connexion

List/shaare/pR6E2Q

  • python
  • python

Lists (list)

Lists are ordered, mutable sequences defined with square brackets []. You can add, remove, or change items after creation.

Characteristics and Use Cases

  • Ordered: items maintain position
  • Mutable: .append(), .insert(), .pop(), .remove()
  • Ideal for storing sequences where order matters and contents change (e.g., list of servers, deployment steps)

Accessing Items and Slicing

  • Access single elements with my_list[index] (0-based). Use negative indices like my_list[-1] for the last item.
  • Slice with my_list[start:stop] to get a sub-list from start up to (but not including) stop.
  • Use three-parameter slicing my_list[start:stop:step] for stepping, e.g., my_list[::2] selects every other element.
  • Omitting start or stop defaults to the beginning or end of the list respectively, and slicing returns a new list without modifying the original.
servers = ["web01", "web02", "web03"]
mixed_list = ["config.yaml", 8080, True]

for item in mixed_list:
    print(type(item))

print(servers[0])
# print(servers[3]) # Commenting this out will raise an IndexError Exception
print(servers[-1])
print(servers[-2])

# Slicing
print(servers[:2]) # Will print only elements at indexes 0 and 1
print(servers[1:]) # Will print only elements at indexes 1 and 2
print(servers[-2:]) # Will print only the second to last and last elements
# Slicing does not alter the original list
print(servers)
# Mutating lists
ports = [80, 443, 8080]
ports.append(5000)
print(ports)
ports.insert(1, 3000)
print(ports)
ports.remove(80)
print(ports)
removed_value = ports.pop(2)
print(ports)
print(removed_value)

# Example to show how mutating lists can lead to side-effects outside of
# the scope of the code that modifies the list.
def mutate_list(l):
    l.pop()

new_list = ["a", "b", "c"]
mutate_list(new_list)
print(new_list)
3 months ago Permalien
cluster icon
  • Signaling Errors: The raise Statement : Signaling Errors: The raise Statement Functions sometimes encounter states they cannot handle and must signal failure clearly. Using raise triggers...
  • Range, zip : Efficient Looping: range Creating large lists for loops is memory-intensive (e.g., list(range(1_000_000))). range() stores only start, stop, and step...
  • Implementing Retries and Timeouts : Implementing Retries and Timeouts External services can be slow or unreliable, causing scripts to hang or fail unexpectedly. Timeouts and retries hel...
  • Making HTTP Requests : Making HTTP Requests The requests library simplifies HTTP interactions by abstracting raw HTTP details, making it ideal for DevOps automation tasks. ...
  • *args and **kwargs : Flexible Functions: *args and **kwargs We can use the syntax *args and **kwargs to accept a variable number of both positional and keyword arguments....


(110)
Filtrer par liens sans tag
Replier Replier tout Déplier Déplier tout Êtes-vous sûr de vouloir supprimer ce lien ? Êtes-vous sûr de vouloir supprimer ce tag ? Le gestionnaire de marque-pages personnel, minimaliste, et sans base de données par la communauté Shaarli