In the Python spirit of “ask for forgiveness, not permission”, here’s one way:
try:
b = a[4]
except IndexError:
b = 'sss'
More Related Contents:
- Why are trailing commas allowed in a list?
- Is it possible to get a list of keywords in Python?
- Reversing a list using slice notation
- Nested list comprehensions
- Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
- Access multiple elements of list knowing their index
- Appending the same string to a list of strings in Python
- Removing item from list causes the list to become NoneType
- Why is [] faster than list()?
- How can I multiply all items in a list together with Python?
- How to unzip a list of tuples into individual lists? [duplicate]
- Remove trailing newline from the elements of a string list
- Getting only element from a single-element list in Python?
- How to loop through all but the last item of a list?
- Writing a list to a file with Python, with newlines
- How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
- How can I initialize a dictionary of distinct empty lists in Python?
- How to read an array of integers from single line of input in python3
- Converting a string that represents a list, into an actual list object
- Merging/adding lists in Python
- how to keep elements of a list based on another list [duplicate]
- Flatten an irregular (arbitrarily nested) list of lists
- How to concatenate (join) items in a list to a single string
- Why does adding a trailing comma after an expression create a tuple?
- “TypeError: ‘type’ object is not subscriptable” in a function signature
- Ellipsis lists […] and concatenating a list to itself [duplicate]
- Loop through list with both content and index [duplicate]
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- Retrieve the two highest item from a list containing 100,000 integers
- Python find list lengths in a sublist