{"id":860,"date":"2024-07-15T20:23:59","date_gmt":"2024-07-15T14:53:59","guid":{"rendered":"https:\/\/www.mrcoder701.com\/?p=860"},"modified":"2024-07-25T16:00:03","modified_gmt":"2024-07-25T10:30:03","slug":"the-ultimate-guide-to-python-lists","status":"publish","type":"post","link":"https:\/\/www.mrcoder701.com\/2024\/07\/15\/the-ultimate-guide-to-python-lists\/","title":{"rendered":"The Ultimate Guide to Python Lists"},"content":{"rendered":"

In the last blog, we learned about The Ultimate Guide to Python Operators<\/a><\/mark>. Today, we\u2019ll dive into one of the most versatile and commonly used data structures in Python: lists. This comprehensive guide will cover everything you need to know about Python lists, from creation to advanced usage, ensuring you have a solid understanding by the end. Let’s get started!<\/p>

What is a Python List?<\/h1>

A Python list is an ordered collection of items that can store multiple values in a single variable. Lists are mutable, meaning you can change their content without changing their identity. They can hold items of different data types, including integers, strings, and even other lists.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Example of a list<\/span><\/span>\nmy_list <\/span>=<\/span> [<\/span>1<\/span>, <\/span>'<\/span>apple<\/span>'<\/span>, <\/span>3.14<\/span>, <\/span>True<\/span>]<\/span><\/span>\nprint<\/span>(my_list) <\/span><\/span>\n<\/span>\n#output<\/span><\/span>\n[<\/span>1<\/span>, <\/span>'<\/span>apple<\/span>'<\/span>, <\/span>3.14<\/span>, <\/span>True<\/span>]<\/span><\/span><\/code><\/pre><\/div>

Here, my_list<\/code> contains an integer, a string, a float, and a boolean value.<\/p>

\n <\/path>\n<\/svg><\/div><\/div>

Creating Lists<\/h1>

Creating a list in Python is straightforward. You can use square brackets or the list()<\/code> constructor.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Creating lists using square brackets<\/span><\/span>\nfruits <\/span>=<\/span> [<\/span>'<\/span>apple<\/span>'<\/span>, <\/span>'<\/span>banana<\/span>'<\/span>, <\/span>'<\/span>cherry<\/span>'<\/span>]<\/span><\/span>\nnumbers <\/span>=<\/span> [<\/span>1<\/span>, <\/span>2<\/span>, <\/span>3<\/span>, <\/span>4<\/span>, <\/span>5<\/span>]<\/span><\/span>\n<\/span>\n# Creating lists using the list() constructor<\/span><\/span>\nletters <\/span>=<\/span> <\/span>list<\/span>([<\/span>'<\/span>a<\/span>'<\/span>, <\/span>'<\/span>b<\/span>'<\/span>, <\/span>'<\/span>c<\/span>'<\/span>, <\/span>'<\/span>d<\/span>'<\/span>])<\/span><\/span>\n<\/span>\nprint<\/span>(<\/span>"<\/span>fruits<\/span>"<\/span>, fruits)<\/span><\/span>\nprint<\/span>(<\/span>"<\/span>numbers <\/span>"<\/span>, numbers )<\/span><\/span>\nprint<\/span>(<\/span>"<\/span>letters <\/span>"<\/span>, letters )<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\nfruits [<\/span>'<\/span>apple<\/span>'<\/span>, <\/span>'<\/span>banana<\/span>'<\/span>, <\/span>'<\/span>cherry<\/span>'<\/span>]<\/span><\/span>\nnumbers  [<\/span>1<\/span>, <\/span>2<\/span>, <\/span>3<\/span>, <\/span>4<\/span>, <\/span>5<\/span>]<\/span><\/span>\nletters  [<\/span>'<\/span>a<\/span>'<\/span>, <\/span>'<\/span>b<\/span>'<\/span>, <\/span>'<\/span>c<\/span>'<\/span>, <\/span>'<\/span>d<\/span>'<\/span>]<\/span><\/span><\/code><\/pre><\/div>

Both methods produce the same result: an ordered collection of items.<\/p>

\n <\/path>\n<\/svg><\/div><\/div>

Accessing List Elements<\/h1>

You can access list elements using indexing and slicing. Indexing starts at 0, so the first element is at index 0.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
fruits <\/span>=<\/span> [<\/span>'<\/span>apple<\/span>'<\/span>, <\/span>'<\/span>banana<\/span>'<\/span>, <\/span>'<\/span>cherry<\/span>'<\/span>]<\/span><\/span>\n# Accessing elements<\/span><\/span>\nprint<\/span>(fruits[<\/span>0<\/span>])  <\/span># Output: apple<\/span><\/span>\nprint<\/span>(fruits[<\/span>2<\/span>])  <\/span># Output: cherry<\/span><\/span>\n<\/span>\n# Negative indexing<\/span><\/span>\nprint<\/span>(fruits[<\/span>-<\/span>1<\/span>])  <\/span># Output: cherry (last item)<\/span><\/span>\nprint<\/span>(fruits[<\/span>-<\/span>3<\/span>])  <\/span># Output: apple (first item)<\/span><\/span><\/code><\/pre><\/div>

Slicing allows you to access a range of elements.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Slicing a list<\/span><\/span>\nprint<\/span>(fruits[<\/span>1<\/span>:<\/span>3<\/span>])  <\/span># Output: ['banana', 'cherry']<\/span><\/span>\nprint<\/span>(fruits[<\/span>:<\/span>2<\/span>])   <\/span># Output: ['apple', 'banana']<\/span><\/span>\nprint<\/span>(fruits[<\/span>1<\/span>:<\/span>])   <\/span># Output: ['banana', 'cherry']<\/span><\/span><\/code><\/pre><\/div>
\n <\/path>\n<\/svg><\/div><\/div>

Modifying Lists<\/h1>

Lists are mutable, so you can add, remove, or change elements.<\/p>

Adding Elements<\/h4>

Use append()<\/code> to add a single element to the end of the list, extend()<\/code> to add multiple elements, and insert()<\/code> to add an element at a specific position.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
fruits <\/span>=<\/span> [<\/span>'<\/span>apple<\/span>'<\/span>, <\/span>'<\/span>banana<\/span>'<\/span>, <\/span>'<\/span>cherry<\/span>'<\/span>]<\/span><\/span>\n<\/span>\n# Adding elements<\/span><\/span>\nfruits.append(<\/span>'<\/span>date<\/span>'<\/span>) <\/span>#<\/span><\/span>\nprint<\/span>(fruits)  <\/span># Output: ['apple', 'banana', 'cherry', 'date']<\/span><\/span>\n<\/span>\nfruits.extend([<\/span>'<\/span>elderberry<\/span>'<\/span>, <\/span>'<\/span>fig<\/span>'<\/span>])<\/span><\/span>\nprint<\/span>(fruits)  <\/span># Output: ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']<\/span><\/span>\n<\/span>\nfruits.insert(<\/span>1<\/span>, <\/span>'<\/span>blueberry<\/span>'<\/span>)<\/span><\/span>\nprint<\/span>(fruits)  <\/span># Output: ['apple', 'blueberry', 'banana', 'cherry', 'date', 'elderberry', 'fig']<\/span><\/span><\/code><\/pre><\/div>

append('date')<\/code> adds 'date'<\/code> to the end of the fruits<\/code> list.<\/p>

extend(['elderberry', 'fig'])<\/code> adds each element of the list ['elderberry', 'fig']<\/code> to the end of the fruits<\/code> list.<\/p>

insert(1, 'blueberry')<\/code> inserts 'blueberry'<\/code> at index 1<\/code>, shifting the current and subsequent elements to the right.<\/p>

Comparison of append<\/code>, insert<\/code>, and extend<\/code><\/h3>
Method<\/th>Syntax<\/th>Description<\/th>Example<\/th>Resulting List<\/th><\/tr><\/thead>
append<\/code><\/td>list.append(element)<\/code><\/td>Adds a single element to the end of the list.<\/td>fruits.append('date')<\/code><\/td>['apple', 'banana', 'cherry', 'date']<\/code><\/td><\/tr>
extend<\/code><\/td>list.extend(iterable)<\/code><\/td>Adds each element of the iterable (e.g., list) to the end of the list.<\/td>fruits.extend(['elderberry', 'fig'])<\/code><\/td>['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']<\/code><\/td><\/tr>
insert<\/code><\/td>list.insert(index, element)<\/code><\/td>Inserts an element at the specified index, shifting subsequent elements.<\/td>fruits.insert(1, 'blueberry')<\/code><\/td>['apple', 'blueberry', 'banana', 'cherry', 'date', 'elderberry', 'fig']<\/code><\/td><\/tr><\/tbody><\/table>
comparison between append, extend and insert<\/figcaption><\/figure>