To Do "Lists" Never End


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

Today: Get hands-on with Python lists β€” master storing and managing of multiple items in one place.

​

If you are an absolute beginner you can glance through previous issues as well

​

A "list" is a datatype that can store multiple values as one variable. Yes you can stuff numbers, strings, anything together

​

πŸ” What does it look like?

Reminder: print() is often used to display output

In [1]: # List is represented by []
# 2,7 & 4 are "items" of the list
numbers = [2, 7, 4]
print(numbers)
Out [1]: [2, 7, 4]

​

πŸ›οΈ As I said anything can be stuffed into a list

In [2]: mix_list = ["Pandas", 2, "Is", 10.0]
print(mix_list)
Out [2]: ['Pandas', 2, 'Is', 10.0]

​

βœ… Verify using "type" if its the right data type

In [3]: type(mix_list)
Out [3]: list

​

🀯 There can be a list of lists too

In [4]: super_list = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(super_list)
Out [4]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

​

πŸ“Š Unlike strings, length of list is the number of items in the list

In [5]: len(mix_list)
Out [5]: 4

​

πŸ”“ Going one level deeper

​

πŸšΆβ€β™‚οΈπŸšΆβ€β™‚οΈ Similar to what we saw for strings yesterday, list items also have positions starting 0

In [6]:# Access 1st item
mix_list[0]
Out [6]: Pandas

​

In [7]:# Access 2nd
mix_list[1]
Out [7]: 2

​

In [8]: # Get first 2 items - 0,1
mix_list[:2]
Out [8]: ['Pandas', 2]

​

In [9]: # Access last element
mix_list[-1]
Out [9]: 10.0

​

πŸ› οΈ Tweaking Lists

​

πŸš€ Add 1 element at the end

In [10]: mix_list.append("awesome")
mix_list
Out [10]: ['Pandas', 2, 'Is', 10.0, 'awesome']

​

πŸš€ Add 1 element at a specific position

In [11]: # "insert" 9 at 2nd position
# Reminder 1 below implies 2nd position
mix_list.insert(1, 9)
mix_list
Out [11]: ['Pandas', 9, 2, 'Is', 10.0, 'awesome']

​

βœ‚οΈ Remove particular value

In [12]: # Remove 9
mix_list.remove(9)
mix_list
Out [12]: ['Pandas', 2, 'Is', 10.0, 'awesome']

​

βœ‚οΈ Remove from specific position

In [13]: # Remove by position
# 1 below means 2nd position
mix_list.pop(1)
mix_list
Out [13]: ['Pandas', 'Is', 10.0, 'awesome']

​

πŸ”„ To Turnaround

In [14]: mix_list.reverse()
mix_list
Out [14]: ['awesome', 10.0, 'Is', 'Pandas']

​

πŸ“Ά Sorting Lists

​

Should be of same datatype.

In [15]: numbers = [2, 7, 4]
numbers.sort()
numbers
Out [15]: [2, 4, 7]

​

In [16]: alphabet_list = [ "Please", "Share", "This", "Newsletter"]
alphabet_list
Out [16]: ['Please', 'Share', 'This', 'Newsletter']
In [17]: alphabet_list.sort()
alphabet_list
Out [17]: ['Newsletter', 'Please', 'Share', 'This']

⁉️ Why mix_list cannot be sorted and will give error. Reply if you know the answer.

​

Use "reverse = True" for sorting in descending order

In [18]: alphabet_list.sort(reverse=True)
alphabet_list
Out [18]: ['This', 'Share', 'Please', 'Newsletter']

​

🀝🀝 Simply add or use extend function to join lists

In [19]: # alphabet_list.extend(numbers) will also give same result
alphabet_list + numbers
Out [19]: ['This', 'Share', 'Please', 'Newsletter', 2, 4, 7]

​

Coming up tomorrow: Concept of Immutability (simple but important concept) and dictionaries.

​

β­πŸ“£ If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Let’s dive into Python "Dictionaries", the toolbox for storing data with labels and values. If you are an absolute beginner you can glance through previous issues as well Think of Python Dictionaries like a real world dictionary: you look up a word (key) and find its meaning (value). Data is basically stored as key-value pair. Use when you want to connect a label (like a name) to information (like...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Dive into Python strings β€” learn how to create, manipulate, and explore them. "String" is any sequence of characters (letters, numbers, symbols etc.) in quotes. Can be represented in single ☝️ or double quotes ✌️ In [1]: # String in single quotes 'I love Pandas Daily' Out [1]: 'I love Pandas Daily' In [2]: # String in double quotes "I love Pandas Daily" Out [2]: 'I love Pandas Daily' ↩️ Slight...

Welcome to the very first issue of Pandas Daily β€” your 5 minute daily dose of Python. Today: Understand some of Python’s data types with examples and clean output. β€œData Types” help the computer understand what you're working withβ€”numbers, text, or something else. Common Data Types 1. Numbers Reminder: As seen below, "In [1]" is the code input we will type in and "Out [1]" is the output of code once executed In [1]: # Integers (anything starting with '#' is a comment and not executed) 8 Out...