Python Data Structures (Lists)

Selin Yazıcıoğlu
4 min readJun 14, 2022

--

Hello guys! I will mention the list which is data structures in this article. I will code on Jupyter Notebook in Python also I will show you on Github. Okay, let’s start!

Data Structures

1- Lists

2- Tuple

3- Set

4- Dictionary

Lists

I would like to explain to features of lists. These are,

  • Accessible with index
  • Ordered
  • Mutable
  • Defined in square brackets. [] List elements separated by commas
  • May contain different types of data. String, int, float
  • They can have two items with the same values, there may be duplicate data

For example,

Also, I want to mention some errors.

If we get ‘Index Error’ when we try to access an item larger than the list length,

list_mix[7]
Output:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 list_mix[7]

NameError: name 'list_mix' is not defined

Indexes must be an integer.

list_mix[4.5] # indexes must be integer.
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 list_mix[4.5]

TypeError: list indices must be integers or slices, not float

Slicing

I want to explain with give examples.

Adding Elements

There are three methods.

  • append()
  • insert()
  • extend()

append()

The append() method appends an element to the end of the list. Explain with a simple an example,

#append():new_list = [87, "Steve Jobs", 9.0]new_list.append("Apple")
print(new_list)
Output:
[87, 'Steve Jobs', 9.0, 'Apple']

insert()

The insert() method inserts the specified value at the specified position.

# insert():
# insert,value
new_list.insert(0,"JumpUp")
print(new_list)
Output:
['JumpUp', 87, 'Steve Jobs', 9.0, 'Apple']

extend()

The extend() method adds the specified list elements (or any iterable) to the end of the current list.

# extend():
new_list.extend([24])
print(new_list)
Output:
['JumpUp', 87, 'Steve Jobs', 9.0, 'Apple', 24]

Extraction Elements

There are four methods.

  • remove()
  • pop()
  • del()
  • clear()

remove()

The remove() method removes the first occurrence of the element with the specified value.

# remove()my_list = ["Spring", "Summer", 12, "Winter", 2022, "Fall"]
my_list.remove("Fall")
print(my_list)
Output:
['Spring', 'Summer', 12, 'Winter', 2022]

pop()

The pop() method removes the element at the specified position.

# pop()my_list = ["Spring", "Summer", 12, "Winter", 2022, "Fall"]
my_list.pop(4)
print(my_list)
Output:
['Spring', 'Summer', 12, 'Winter', 'Fall']

del()

The del the keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, parts of a list, etc.

# del()my_list = ["Spring", "Summer", 12, "Winter", 2022, "Fall"]del my_list[0:2]
print(my_list)
Output:
[12, 'Winter', 2022, 'Fall']

clear()

The clear() method removes all the elements from a list.

# clear() 
my_list = [True, "Python", 12]
my_list.clear()
print(my_list)
Output:
[]

Finding an element in a list.

# Finding an element in a list.my_list = ["Minions", "Gru", 12, "Agnes", 2022, "Jessica", "Mexico"]
print(my_list.index("Mexico"))
Output:
6
# Concating
# We can combine it with the = operator or the extend() method.
list1 = [3,4,5]
list2 = [9,20,60]
list_new = list1 + list2
print(list_new)
Output:
[3, 4, 5, 9, 20, 60]
list1.extend(list2)
print(list1)
Output:
[3, 4, 5, 9, 20, 60]
list2.extend(list1)
print(list2)
Output:
[9, 20, 60, 3, 4, 5, 9, 20, 60]

Copying Lists

There are two methods. These are,

  • = operator
  • copy()

I would like to explain with two examples.

my_list1 = [1,2,3]
new_list = my_list1
print(new_list)
Output:
[1, 2, 3]
my_list1 = [1,2,3]
new_list = my_list1.copy()
print(new_list)
print(my_list1)
Output:
[1, 2, 3]
[1, 2, 3]

Lists Methods

  • sort()
  • reverse()
  • max,min()
  • sum()

I want to explain that give an example.

my_list = [1,4,7,3,9]
my_list.sort()
print("Sorted the list: ", my_list)
my_list.reverse()
print("Reverse order of the list: ", my_list)
print("Maximum element of the list: ", max(my_list))
print("Minimum element of the list: ", min(my_list))
print("Sum of the list: ", sum(my_list))
Output:
Sorted the list: [1, 3, 4, 7, 9]
Reverse order of the list: [9, 7, 4, 3, 1]
Maximum element of the list: 9
Minimum element of the list: 1
Sum of the list: 24

In addition, if we want to change any element in the list then,

# We changed the element in the second index.favorite_latin_singer = ["Gente De Zona", "Ricky Martin", "Elvis Crespo","Daddy Yankee", "Enrique Iglesias", "Carlos Vives"]
favorite_latin_singer[2] = "Yandel"
print(favorite_latin_singer)
Output:
['Gente De Zona', 'Ricky Martin', 'Yandel', 'Daddy Yankee', 'Enrique Iglesias', 'Carlos Vives']

Practicing

I hope while you’re reading my article you can enjoy it!

See you on another topic :)

If you want, you can follow me at these links:

--

--