0% found this document useful (0 votes)
5 views

Python Lists Tuples Sets Guide

The document provides a comprehensive guide on Python Lists, Tuples, and Sets, detailing their characteristics, syntax, common operations, and examples. Lists are ordered, mutable, and allow duplicates; tuples are ordered, immutable, and also allow duplicates; while sets are unordered, mutable, and do not allow duplicates. It also includes conversion methods between these data types and a comparison table highlighting their features.

Uploaded by

drdeath0002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Lists Tuples Sets Guide

The document provides a comprehensive guide on Python Lists, Tuples, and Sets, detailing their characteristics, syntax, common operations, and examples. Lists are ordered, mutable, and allow duplicates; tuples are ordered, immutable, and also allow duplicates; while sets are unordered, mutable, and do not allow duplicates. It also includes conversion methods between these data types and a comparison table highlighting their features.

Uploaded by

drdeath0002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Lists, Tuples, and Sets - Complete Guide

Python Lists, Tuples, and Sets - Complete Guide

LISTS

- Ordered, mutable, allows duplicates

- Syntax: my_list = [1, 2, 3]

Common operations:

- my_list[0], my_list.append(x), my_list.remove(x), len(my_list), my_list.sort(), etc.

Example:

fruits = ["apple", "banana"]

fruits.append("orange")

print(fruits) # ['apple', 'banana', 'orange']

TUPLES

- Ordered, immutable, allows duplicates

- Syntax: my_tuple = (1, 2, 3)

Common operations:

- my_tuple[0], my_tuple.count(x), my_tuple.index(x), len(my_tuple)

Example:

person = ("Alice", 30)

print(person[0]) # Alice

SETS

- Unordered, mutable, no duplicates

- Syntax: my_set = {1, 2, 3}

Common operations:
Python Lists, Tuples, and Sets - Complete Guide

- my_set.add(x), my_set.remove(x), set1 | set2, set1 & set2

Example:

colors = {"red", "green"}

colors.add("blue")

print(colors) # {'red', 'green', 'blue'}

CONVERSION

List to Tuple: tuple(my_list)

Tuple to List: list(my_tuple)

List to Set: set(my_list)

Set to List: list(my_set)

Tuple to Set: set(my_tuple)

Set to Tuple: tuple(my_set)

Use sorted() to maintain order when needed:

sorted(list(set))

COMPARISON TABLE

Feature | List | Tuple | Set

----------------------|--------|--------|-------

Ordered | Yes | Yes | No

Mutable | Yes | No | Yes

Allows Duplicates | Yes | Yes | No

Indexing/Slicing | Yes | Yes | No

Performance | Medium | Fast | Medium

Use Case | General| Fixed | Unique items

You might also like