Adding Elements to a List in Python
In Python, one can easily add new elements to a list using the `append` method.
namelist.append(item)
The `append` method places the new element at the end of the list:
- `namelist` refers to the list you're adding to.
- `item` represents the value you wish to add, which can be either numeric or alphanumeric.
Note. It's important to note that the new element is positioned immediately after the last item in the list.
A practical example
Start by creating an empty list named `city`:
city=[]
Add an element to the list using the `append` method:
city.append('New York')
Add another element to the end:
city.append('Rome')
To view the list's contents:
>>> city
['New York', 'Rome']
As shown, the list now contains two elements.
https://how.okpedia.org/en/python/how-to-add-elements-to-a-python-list
Report an error or share a suggestion to enhance this page