Saturday, 9 March 2019

Working with Lists in Python



Working with Lists in Python – A Complete Beginner’s Guide

If you learn only one data structure in Python, make it a list.

Why? Because almost everything you build in Python—apps, scripts, automation, data analysis—relies on lists.

Lists are:

  • Easy to use

  • Extremely powerful

  • Used everywhere

Let’s master them step by step 👇


What is a List in Python?

A list is a collection of items stored in a single variable.

numbers = [1, 2, 3, 4, 5]

A list can store:

  • Integers

  • Strings

  • Floats

  • Even other lists

data = [10, "Python", 3.14, True]

Accessing List Elements

Each element has an index (starting from 0).

langs = ["Python", "Java", "C++"]
print(langs[0]) # Python
print(langs[2]) # C++

Negative Indexing

print(langs[-1]) # C++

Modifying List Values

Lists are mutable (you can change them).

langs[1] = "JavaScript"
print(langs)

Output:

['Python', 'JavaScript', 'C++']

Common List Operations

1. Adding Elements

lst = [1, 2]
lst.append(3)
lst.insert(0, 99)

2. Removing Elements

lst.remove(99)
lst.pop()
del lst[0]

3. Length of List

len(lst)

Looping Through a List

Basic Loop

for item in lst:
print(item)

With Index

for i in range(len(lst)):
print(i, lst[i])

Slicing Lists

Extract parts of a list:

nums = [10, 20, 30, 40, 50]
print(nums[1:4])

Output:

[20, 30, 40]

Useful List Methods

MethodPurpose
append()Add at end
insert()Add at index
remove()Remove value
pop()Remove last
sort()Sort list
reverse()Reverse
count()Count value
index()Find position

Example:

nums.sort()
nums.reverse()

List Comprehension (Power Feature)

One line instead of loops:

squares = [x*x for x in range(5)]
print(squares)

Output:

[0, 1, 4, 9, 16]

Nested Lists (2D Lists)

matrix = [
[1, 2],
[3, 4]
]

print(matrix[1][0]) # 3

Used in:

  • Tables

  • Matrices

  • Game boards


Common Beginner Mistake

This does NOT modify the list:

arr = [1, 2, 3]
for i in arr:
i = i * 10
print(arr)

Output:

[1, 2, 3]

Correct way:

for i in range(len(arr)):
arr[i] *= 10

Why Lists Are So Important

Lists are used in:

  • Web scraping

  • Data science

  • Machine learning

  • Automation

  • Game development

  • APIs & JSON

If you know lists well, you already know 40% of Python.


Final Thoughts

Mastering lists means:

  • Cleaner code

  • Faster logic

  • Better problem-solving

Before learning:

  • NumPy

  • Pandas

  • Machine Learning

👉 First, become deadly with Python lists.

Because every advanced concept is built on top of them. 🚀

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (219) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (9) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (86) Coursera (300) Cybersecurity (29) data (4) Data Analysis (27) Data Analytics (20) data management (15) Data Science (322) Data Strucures (16) Deep Learning (133) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (66) Git (10) Google (50) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (262) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1264) Python Coding Challenge (1074) Python Mistakes (50) Python Quiz (443) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)