How to append multiple values to a list in Python
How to append multiple values to a list in Python
You can use the sequence method list.extend
to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.
>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> lst.extend(range(11, 14))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
So you can use list.append()
to append a single value, and list.extend()
to append multiple values.
Other than the append
function, if by multiple values you mean another list, you can simply concatenate them like so.
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
How to append multiple values to a list in Python
If you take a look at the official docs, youll see right below append
, extend
. Thats what your looking for.
Theres also itertools.chain
if you are more interested in efficient iteration than ending up with a fully populated data structure.
Related posts:
- How to extract values or data from a list of stored links using selenium python?
- python – append multiple values for one key in a dictionary
- How to append multiple values to a list in Python
- pandas – Returning multiple values in python and appending them to unique columns to a dataframe
- python – How to concatenate multiple column values into a single column in Pandas dataframe
- ValueError: need more than 0 values to unpack (python lists)
- python – Splitting dataframe into multiple dataframes
- algorithm – Find indices of elements with equal values in a matrix (Python)
- python – Google Docs API Batch Update – multiple updates to same template file?
- sqlite – TD Ameritrade API multiple quotes string Async io message problem