Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python Lists and Tuples Fundamentals

Tech 1

Lists

A list is an ordered collection of elements that can hold items of different types.

Key characteristics:

  • Elements: Can be of any data type.
  • Order: Maintains a specific sequence.

Creating Lists

There are two primary ways to create lists:

Method 1: Initialize an empty list and add elements one by one.

>>> devices = list()
>>> devices.append("Samsung")
>>> devices.append("Sony")
>>> devices.append("LG")
>>> devices
['Samsung', 'Sony', 'LG']

Method 2: Define the list directly with initial values.

>>> devices = ["Samsung", "Sony", "LG"]
>>> devices
['Samsung', 'Sony', 'LG']

Manipulating Lists

Retrieving Elements

Access elements using index notation [i], where i starts from zero.

>>> devices = ["Samsung", "Sony", "LG"]
>>> devices[0]
'Samsung'
>>> devices[1]
'Sony'
>>> devices[2]
'LG'

Use the index() method to find the first occurrence of a value.

>>> devices = ["Samsung", "Sony", "LG", "Sony"]
>>> devices.index("Sony")
1

Adding Elements

Several methods exist for appending elements:

  • append(): Adds an element to the end of the list.
>>> devices = []
>>> devices
[]
>>> devices.append("Samsung")
>>> devices
['Samsung']
>>> devices.append("Sony")
>>> devices
['Samsung', 'Sony']
  • insert(index, item): Inserts an element at a specified position.
>>> devices = ["Samsung", "Sony", "LG"]
>>> devices.insert(1, "Apple")
>>> devices
['Samsung', 'Apple', 'Sony', 'LG']
  • extend(): Appends all elements from another iterable to the list.
>>> devices = ["Samsung", "Sony", "LG"]
>>> more_devices = ["Apple", "Nokia"]
>>> devices.extend(more_devices)
>>> devices
['Samsung', 'Sony', 'LG', 'Apple', 'Nokia']

Modifying Elements

Direct assignment replaces an existing element.

>>> devices = ["Samsung", "Sony", "LG"]
>>> devices[1] = "Apple"
>>> devices
['Samsung', 'Apple', 'LG']

Removing Elements

Various techniques are available for removing elements:

  • pop([index]): Removes and returns an element at a given index (default is last).
>>> devices = ["Samsung", "Sony", "LG"]
>>> devices.pop()
'LG'
>>> devices.pop(0)
'Samsung'
>>> devices
['Sony']
  • remove(value): Deletes the first occurrence of a value.
>>> devices = ["Samsung", "Sony", "LG", "Sony"]
>>> devices.remove("Sony")
>>> devices
['Samsung', 'LG', 'Sony']
  • clear(): Empties the entire list.
>>> devices = ["Samsung", "Sony", "LG"]
>>> devices.clear()
>>> devices
[]
  • del: Removes elements by slicing or index.
>>> devices = ["Samsung", "Sony", "LG"]
>>> del devices[:]
>>> devices
[]

List Reversal

Two approaches to reverse a list:

Using reverse() method:

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers.reverse()
>>> numbers
[5, 4, 3, 2, 1]

Using slicing:

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[::-1]
[5, 4, 3, 2, 1]

The difference between these methods is:

  • reverse() modifies the original list in place.
  • Slicing creates a new reversed list without altering the original.

Sorting Lists

Lists support built-in sorting via the sort() method.

>>> data = [4, 8, 1, 7, 2]
>>> data.sort()
>>> data
[1, 2, 4, 7, 8]

Tuples

Tuples are immutable sequences, typically containing heterogeneous elements.

Creating Tuples

Method 1: Use parentheses to define a tuple.

>>> atuple = (1, 2, 3, 4)
>>> atuple
(1, 2, 3, 4)

Method 2: Parentheses are optional when creating tuples.

>>> btuple = 1, 2, 3, 4
>>> btuple
(1, 2, 3, 4)

To create a single-element tuple, include a trailing comma:

>>> ctuple = (1,)
>>> type(ctuple)
<class 'tuple'>
>>> ctuple
(1,)

>>> dtuple = 1,
>>> type(dtuple)
<class 'tuple'>
>>> dtuple
(1,)

A empty tuple can be created like so:

>>> a = tuple()
>>> a
()
>>> b = ()
>>> b
()

Since tuples are immutable, they do not support modification operations.

Converting Between Lists and Tuples

Converting from tuple to list:

>>> atuple = (1, 2, 3, 4)
>>> list(atuple)
[1, 2, 3, 4]

Converting from list to tuple:

>>> alist = [1, 2, 3, 4]
>>> tuple(alist)
(1, 2, 3, 4)

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.