#9 Lists / Python Lessons for Beginners

by Alex
#9 Lists / Python Lessons for Beginners

Previous lesson: Operators in Python In the Python programming language, there are four types of data for storing sequences:

  • List – An ordered sequence that can be changed. Identical elements are allowed.
  • Tuple – A sequence that is ordered but not modifiable. Identical elements are allowed.
  • Set – A sequence that is unordered but not modifiable. Unsimilar items are deleted.
  • Dict (dictionary) – an unordered variable sequence consisting of key and value pairs. The keys are not duplicated.

When choosing a sequence type, it is helpful to know and understand the properties of each type. Choosing the right type for a particular set of data helps to preserve meaning, and it gives you an increase in efficiency or security.

Lists

Lists are ordered and changeable sequences. In Python, lists are written in square brackets. List creation:

thislist = ["apple", "banana", "cherry"]
print(thislist)

Output:

["apple", "banana", "cherry"]

Accessing list items

You access the items in the list by referring to the index number. Let’s output the second element of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Output:

"banana"

How to change a value by index

To change the value of a particular element, reference the index number. Change the second element:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "currants"
print(thislist)

Output:

["apple," "currant," "cherry"]

Iterating through a list

You can iterate through the list items with a forloop Output all elements in the list, one by one:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)

Output:

apple
banana
cherry

You can learn more about the for loop by reading the Python for loop section.

List length

To determine how many list items you have, use the len()method Output the number of elements in the list:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

Output:

3

Add items

To add items to the end of the list, use the append()method

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Output:

["apple", "banana", "cherry", "orange"]

To add an element by the specified index, use the insert() method: Insert the element as the second position:

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

Output:

["apple", "orange", "banana", "cherry"]

Deleting items

There are several methods of removing list items The remove() method removes specific items:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Output:

["apple", "cherry"]

The pop() method removes an element by index (or the last element if no index is specified) and returns it:

thislist = ["apple", "banana", "cherry"]
last_element = thislist.pop()
print(thislist)
print(last_element)

Output:

["apple", "banana"]
"cherry"

The del keyword removes a specific index:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

Output:

["banana", "cherry"]

The del keyword can remove the list completely:

thislist = ["apple", "banana", "cherry"]
del thislist
print(thislist) # this causes an error because "thislist" no longer exists.

The clear() method clears the list:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

Output:

[]

The list() constructor

You can also use the list() constructor to create a list.

thislist = list(("apple", "banana", "cherry"))  # notice the double parentheses
print(thislist)

Conclusion:

["apple", "banana", "cherry"]

List Methods

More on the use of list methods:

  • Python lists – Python list() functions and methods

Python has a set of built-in methods that you can use when working with lists: MethodValuearound()Adds item(s) to the end of the listcreard()Removes all items in the listcoru()Returns a copy of the listcount()Returns the number of items with a certain valueextd()Returns the number of items with a certain valueextd()Adds items to the end of the current listinԁex()Returns the index of the first item with a specific valueinnsert()Adds an item by indexerror()Deletes items by index or last item Next: Tuple

Related Posts

LEAVE A COMMENT