OKPEDIA PYTHON LISTA EN

How to add elements to list in Python

To insert a new element into a Python list, you can use the append method.

namelist.append(item)

The append method adds the new element to the end of the list.

  • The namelist argument is the name of the list.
  • The item argument is the value to be added to the list. It can be numeric or alphanumeric.

Note. The new element is added after the last element of the list.
the new element is added at the bottom of the list

A practical example

Create an empty list.

The list is called city.

city=[]

Add an element in the list with the method append.

city.append('New York')

Add another item at the end of the list.

city.append('Rome')

View the contents of the list

>>> city
['New York', 'Rome']

There are two elements in the list.

https://how.okpedia.org/en/python/how-to-add-elements-to-a-python-list


Report us an error or send a suggestion to improve this page


Python List


FacebookTwitterLinkedinLinkedin