Version 1.0. Prepared by Makzan.
In this example, we explore different ways to store multiple- collections. They are:
If we do not need to separate the multiple columns, we dont necessary need to separate them.
A list simply fulfils the job:
import random
quotes = (
"I want to put a ding in the universe.—Steve Jobs",
"Life is 10% what happens to you and 90% how you react to it.—Charles R. Swindoll",
"Family is not an important thing. It's everything.—Michael J. Fox",
"Nothing is impossible, the word itself says 'I'm possible'!—Audrey Hepburn",
"There are two ways of spreading light: to be the candle or the mirror that reflects it.—Edith Wharton",
"Try to be a rainbow in someone's cloud.—Maya Angelou",
"Be brave enough to live life creatively. The creative place where no one else has ever been.—Alan Alda",
"The secret of getting ahead is getting started.—Mark Twain",
)
print( random.choice(quotes) )
There are two ways of spreading light: to be the candle or the mirror that reflects it.—Edith Wharton
Given that the data is only a list of string. Each string contains two data: the quote content and who was saying that. They are separated by a dash —
.
We can use split()
to split any string by given "separator".
For instance, we can use split("—")
.
import random
quotes = (
"I want to put a ding in the universe.—Steve Jobs",
"Life is 10% what happens to you and 90% how you react to it.—Charles R. Swindoll",
"Family is not an important thing. It's everything.—Michael J. Fox",
"Nothing is impossible, the word itself says 'I'm possible'!—Audrey Hepburn",
"There are two ways of spreading light: to be the candle or the mirror that reflects it.—Edith Wharton",
"Try to be a rainbow in someone's cloud.—Maya Angelou",
"Be brave enough to live life creatively. The creative place where no one else has ever been.—Alan Alda",
"The secret of getting ahead is getting started.—Mark Twain",
)
quote = random.choice(quotes).split("—")
print(f'{quote[1]} said: {quote[0]}')
Steve Jobs said: I want to put a ding in the universe.
Line splitting may be used often when we cannot control the data source. Here is the pros and cons of using string splitting:
Pros:
Cons:
We can split string by using split
. On the other hand, we can merge a list
string = "Steven said: Hello World"
string.split(": ")
['Steven said', 'Hello World']
sample_list = ['Steven said', 'Hello World']
": ".join(sample_list)
'Steven said: Hello World'
import random
quotes = (
("I want to put a ding in the universe.", "Steve Jobs"),
("Life is 10% what happens to you and 90% how you react to it.", "Charles R. Swindoll"),
("Family is not an important thing. It's everything.", "Michael J. Fox"),
("Nothing is impossible, the word itself says 'I'm possible'!", "Audrey Hepburn"),
("There are two ways of spreading light: to be the candle or the mirror that reflects it.", "Edith Wharton"),
("Try to be a rainbow in someone's cloud.", "Maya Angelou"),
("Be brave enough to live life creatively. The creative place where no one else has ever been.", "Alan Alda"),
("The secret of getting ahead is getting started.", "Mark Twain"),
)
quote = random.choice(quotes)
print(f'{quote[1]} said: {quote[0]}')
Edith Wharton said: There are two ways of spreading light: to be the candle or the mirror that reflects it.
Pros and cons of nested list
Pros:
Cons:
import random
quotes = (
{"quote":"I want to put a ding in the universe.", "source":"Steve Jobs"},
{"quote":"Life is 10% what happens to you and 90% how you react to it.", "source":"Charles R. Swindoll"},
{"quote":"Family is not an important thing. It's everything.", "source":"Michael J. Fox"},
{"quote":"Nothing is impossible, the word itself says 'I'm possible'!", "source":"Audrey Hepburn"},
{"quote":"There are two ways of spreading light: to be the candle or the mirror that reflects it.", "source":"Edith Wharton"},
{"quote":"Try to be a rainbow in someone's cloud.", "source":"Maya Angelou"},
{"quote":"Be brave enough to live life creatively. The creative place where no one else has ever been.", "source":"Alan Alda"},
{"quote":"The secret of getting ahead is getting started.", "source":"Mark Twain"},
)
quote = random.choice(quotes)
content = quote["quote"]
source = quote["source"]
print(f'{source} said: {content}')
Alan Alda said: Be brave enough to live life creatively. The creative place where no one else has ever been.
Pros and cons of using dictionary:
Pros:
Cons:
A key typo can result in error.
quote["sooooource"]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-3-9197b19c2227> in <module> ----> 1 quote["sooooource"] KeyError: 'sooooource'
We may avoid key error by using .get
. But it still does not prevent typo defined from source.
quote.get("source")
'Alan Alda'
quote.get("soooource", "Not found")
'Not found'
import random
from collections import namedtuple
Quote = namedtuple("Quote", "content, source")
quotes = (
Quote(content="I want to put a ding in the universe.", source="Steve Jobs"),
Quote(content="Life is 10% what happens to you and 90% how you react to it.", source="Charles R. Swindoll"),
Quote(content="Family is not an important thing. It's everything.", source="Michael J. Fox"),
Quote(content="Nothing is impossible, the word itself says 'I'm possible'!", source="Audrey Hepburn"),
Quote(content="There are two ways of spreading light: to be the candle or the mirror that reflects it.", source="Edith Wharton"),
Quote(content="Try to be a rainbow in someone's cloud.", source="Maya Angelou"),
Quote(content="Be brave enough to live life creatively. The creative place where no one else has ever been.", source="Alan Alda"),
Quote(content="The secret of getting ahead is getting started.", source="Mark Twain"),
)
quote = random.choice(quotes)
print(f'{quote.source} said: {quote.content}')
Alan Alda said: Be brave enough to live life creatively. The creative place where no one else has ever been.
Pros and cons of using namedtuple
Pros
Cons
Question 1: Assuming now we want to store a list of names. Just names and nothing else. We may need to expand the list later. Which data structure shall we use?
Question 2: Assuming now we want to store a list of student records. Each record contains first name, last name, and email. Which data structure shall we use?
In this section, we explored different way to store multi-columns data. It includes:
In future, we will further use pandas DataFrame and reading data from CSV and Excel for tabular data.