List
A List is an ordered collection of items that can store different types of data such as numbers, strings, or even other lists. Lists are mutable, which means their contents can be modified after creation.
- Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
- List is a built-in data structure used to store an ordered collection of items. They are dynamic, resizable and capable of storing multiple data types.
- Mutable: list elements can be changed, updated, added, or removed after the list is created.
- Ordered: elements maintain the order in which they are inserted.
- Index-based: elements are accessed using their position, starting from index 0.
a = [1, 2, 3]
print(a)
b = ["apple", "banana"]
print(b)
a = list((1, 2, 3, 'apple', 4.5))
print(a)
b = list("GFG")
print(b)
Output
[1, 2, 3]
['apple', 'banana']
[1, 2, 3, 'apple', 4.5]
['G', 'F', 'G']
Method Description
- append() Adds an element at the end of the list
- clear() Removes all the elements from the list
- copy() Returns a copy of the list
- count() Returns the number of elements with the specified value
- extend() Add the elements of a list (or any iterable), to the end of the current list
- index() Returns the index of the first element with the specified value
- insert() Adds an element at the specified position
- pop() Removes the element at the specified position
- remove() Removes the item with the specified value
- reverse() Reverses the order of the list
- sort() Sorts the list
Use Lists When:
- Data needs frequent updates.
- Elements need to be added or removed.
- Working with dynamic datasets.
Tuples
A Tuple is also an ordered collection of items, but unlike lists, tuples are immutable. Once a tuple is created, its values cannot be changed.
- Tuples are similar to lists, but unlike lists, they cannot be changed after their creation.
- Can hold elements of different data types.
- These are ordered, heterogeneous and immutable
Tuples can store elements of different data types, such as integers, strings, lists and dictionaries, within a single structure.
Tuples can be concatenated using the + operator. This operation combines two or more tuples to create a new tuple
Method Description
- count() Returns the number of times a specified value occurs in a tuple
- index() Searches the tuple for a specified value and returns the position of where it was found
tup = ()
print(tup)
# Using String
tup = ('Geeks', 'For')
print(tup)
# Using List
li = [1, 2, 4, 5, 6]
print(tuple(li))
# Using Built-in Function
tup = tuple('Geeks')
print(tup)
Output
()
('Geeks', 'For')
(1, 2, 4, 5, 6)
('G', 'e', 'e', 'k', 's')
Slicing of Tuple
Slicing a tuple means creating a new tuple from a subset of elements of the original tuple. The slicing syntax is tuple[start:stop:step].
tup = tuple('GEEKSFORGEEKS')
print(tup[1:])
print(tup[::-1])
print(tup[4:9])
Output
('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S')
('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G')
('S', 'F', 'O', 'R', 'G')
Use Tuples When:
Data should remain unchanged.
Storing fixed information.
Better performance is required.
Lists and Tuples are fundamental data structures in Python. Lists provide flexibility because they can be modified, while Tuples provide safety and better performance because they cannot be changed. Choosing the right data structure depends on the application's requirements. Understanding these concepts helps programmers write more efficient and organized Python code.
References
https://www.w3schools.com/python/python_lists.asp
https://www.geeksforgeeks.org/python/python-lists/
https://www.geeksforgeeks.org/python/python-tuples/
https://www.w3schools.com/python/python_tuples.asp
Top comments (0)