List are only populated by append()?

Hi every one.

I am practising with lists exercicies by my own and I find that the methods associated with the list object are these:

Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

source: www.w3schools.com

The question:

if only I need to use the items from the above lists, or I can do the same job as in dictionaries case, where I have:

dictionary_name[key] = and asign the value.

Thank you very very much.

A.

I would recommend looking at the official python documentation as well. Just in case an alternative resource is incomplete.

Yes, that’s correct. Only official methods associated with lists can be used to carry out any sort of operation on them.

In relation to lists, the equivalent of the above would be indexing. Where key would be an index instead. And you won’t be adding anything to the list. You will only be replacing an existing value at that index with a new value.

a = [5]
a[2] = 3

would throw an error because a doesn’t have anything at index 2. And

a[0] = 6

would change the 5 to a 6.

List are only populated by append()?

No. As the documentation states, extend() can be used to append elements of a different list to the end of the current list.

So,

a = [5]
b = [6]
a.extend(b)

a.extend(b) would result in a being [5, 6]

1 Like

Wauw!

No words.
Again, plaisir be here!

Thx.

A.