3. Working with data collections
English translation
Contents
3.1 Lists
- A list groups multiple data items in 1 column.\
- A list starts and ends with
[ ]and can contain different data types. (Data types are covered in more detail in the next section.)
list_mixed = [0, 2.5, 'abc']- Lists can be created in various ways, as shown below.
# Create an empty list
empty_list = []
print(empty_list)[]
# Create by entering values directly
list_manual = [0, 1, 2, 3, 4]
print(list_manual)[0, 1, 2, 3, 4]
# Create using range (A, B, C below are all integers)
# range(B) - create a list from 0 in steps of 1 up to integers less than B
list_new = list(range(10))
print(list_new)
# range(A,B) - create a list starting at A in steps of 1 up to integers less than B
list_new = list(range(1,10))
print(list_new)
# range(A,B,C) - create a list starting at A in steps of C up to integers less than B (reverse for negative C)
list_new = list(range(2,10,2))
print(list_new)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 4, 6, 8]
- The
+and*operations behave somewhat differently for lists.- The
+operation concatenates the lists on its left and right. - The
*operation repeats the list to the left of*the number of times specified on the right.
- The
# + and * operations on lists
list_manual = [0, 1, 2, 3, 4]
list_new = list(range(4,-1,-1))
print(list_manual+list_new)
print(list_manual*2)[0, 1, 2, 3, 4, 4, 3, 2, 1, 0] [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
- Lists can be modified using built-in functions, as in the examples below.
# Append an element to the end of a list
list_manual = [0, 1, 2, 3, 4]
list_manual.append(5)
print(list_manual)[0, 1, 2, 3, 4, 5]
# Add an element at a specified position in a list
# insert(position, value) Note that the starting position is 0
list_manual.insert(2, 10)
print(list_manual)[0, 1, 10, 2, 3, 4, 5]
# Remove an element from the end of a list
list_manual.pop()
print(list_manual)[0, 1, 10, 2, 3, 4]
# Remove an element at a specified position in a list
# pop(position)
list_manual.pop(2)
print(list_manual)[0, 1, 2, 3, 4]
- Various other useful functions are also provided by default.
# Count the elements in a list
num_data = len(list_manual)
print(num_data)5
# Find an element's position (return the first occurrence)
position = list_manual.index(2)
print(position)2
# Sort a list by value (ascending by default)
list_manual.sort()
print(list_manual)
list_manual.sort(reverse=True)
print(list_manual)[0, 1, 2, 3, 4] [4, 3, 2, 1, 0]
# Select a desired segment (slicing)
# Select and return one element
print(list_manual[2])2
# [A:B] Select positions from A up to, but excluding, B
# [2:4] = select positions 2,3
print(list_manual[2:4])[2, 1]
# [A:] From position A to the end
# [:B] Select from the start up to, but excluding, B
print(list_manual[2:])
print(list_manual[:4])[2, 1, 0] [4, 3, 2, 1]
# [-A] The Ath element from the end
print(list_manual[-2])1
3.2 Tuples
- A tuple is like a list, but cannot be modified after it is created.
- A tuple starts and ends with
( ); otherwise, its notation is the same as a list.
# Create an empty tuple = effectively pointless, since it cannot be modified.
empty_tuple = ()
print(empty_tuple)()
# Create by entering values directly
tuple_manual = (0, 1, 2, 3, 4)
print(tuple_manual)(0, 1, 2, 3, 4)
# Create using range (A, B, C below are all integers)
print(tuple(range(2,10,2)))(2, 4, 6, 8)
- Except for functions that modify data, the other available functions are almost the same as those for lists.
len(tuple_manual)5
tuple_manual.index(4)4
tuple_manual[1:](1, 2, 3, 4)
3.3 Sets
- A set is similar to a list, but does not allow duplicate elements.
- A set starts and ends with
{ }; otherwise, its notation is the same as a list.
# Create an empty set
empty_set = {}
print(empty_set){}
# Create by entering values directly - duplicates are removed.
set_manual = {0, 1, 2, 3, 4, 4, 3, 2, 1, 0}
print(set_manual){0, 1, 2, 3, 4}
# Create using range (A, B, C below are all integers)
print(set(range(2,10,2))){8, 2, 4, 6}
- Since a set is a mathematical set, most mathematical set operations are also available.
- For example,
# Intersection '&'
set_first = {1, 3, 5}
set_second = {1, 2, 4, 8}
print(set_first&set_second){1}
# Union '|'
print(set_first|set_second){1, 2, 3, 4, 5, 8}
# Difference '-'
print(set_first-set_second){3, 5}
# Check whether it is a subset
# A.issuperset(B) - whether B is a subset of A (whether A contains B)
set_third = {1, 3, 5, 7}
set_third.issuperset(set_first)True
# Check membership
3 in set_firstTrue
3.4 Dictionaries
- A dictionary is similar to a list, but positions (indices) are managed using arbitrary values called 'keys' instead of integers;
- Add an item
- Add an item
is managed.
- Like a set, a dictionary is enclosed in
{ }, but each entry is stored as a key:value pair.
my_dictionary = {'name':'John', 'age':15, 'height': 1.76}
print(my_dictionary){'name': 'John', 'age': 15, 'height': 1.76}
- Selection is also similar to lists, but access uses a 'key' instead of a position (index).
my_dictionary['name']'John'
- A dictionary can be an element of a list, and vice versa.
first_user = {'name':'John', 'age':15, 'height':1.76, 'position':[15,3]}
second_user = {'name':'Paul', 'age':17, 'height':1.88, 'position':[-2,7]}
users = [first_user, second_user]
print(users[0]['name'])John