Python Lists | ||||||||||||||||||||||
| ||||||||||||||||||||||
Lists are used to store multiple items in a single variable. 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. Lists are created using square brackets:
List Items List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index Ordered When we say that lists are ordered, it means that the items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list. Changeable The list is changeable, meaning that we can change, add, and remove items in a list after it has been created. Allow Duplicates Since lists are indexed, lists can have items with the same value:
List Length To determine how many items a list has, use the
List Items - Data Types List items can be of any data type:
A list can contain different data types:
type() From Python's perspective, lists are defined as objects with the data type 'list':
The list() Constructor It is also possible to use the list() constructor when creating a new list.
Python Collections (Arrays) There are four collection data types in the Python programming language:
Access Items List items are indexed and you can access them by referring to the index number:
Negative Indexing Negative indexing means start from the end
Range of Indexes You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.
By leaving out the start value, the range will start at the first item:
By leaving out the end value, the range will go on to the end of the list:
Range of Negative Indexes Specify negative indexes if you want to start the search from the end of the list:
Check if Item Exists To determine if a specified item is present in a list use the
Change Item Value To change the value of a specific item, refer to the index number:
Change a Range of Item Values To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values:
If you insert more items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly:
If you insert less items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly:
Insert Items To insert a new list item, without replacing any of the existing values, we can use the The
Append Items To add an item to the end of the list, use the append() method:
Insert Items To insert a list item at a specified index, use the The
Extend List To append elements from another list to the current list, use the
Add Any Iterable The
Remove Specified Item The
If there are more than one item with the specified value, the
Remove Specified Index The
If you do not specify the index, the
The
The
Clear the List The The list still remains, but it has no content.
Loop Through a List You can loop through the list items by using a
Loop Through the Index Numbers You can also loop through the list items by referring to their index number. Use the
The iterable created in the example above is Using a While Loop You can loop through the list items by using a Use the Remember to increase the index by 1 after each iteration.
Looping Using List Comprehension List Comprehension offers the shortest syntax for looping through lists:
Sort List Alphanumerically List objects have a
Sort Descending To sort descending, use the keyword argument
Customize Sort Function You can also customize your own function by using the keyword argument The function will return a number that will be used to sort the list (the lowest number first):
Case Insensitive Sort By default the
Luckily we can use built-in functions as key functions when sorting a list. So if you want a case-insensitive sort function, use str.lower as a key function:
Reverse Order What if you want to reverse the order of a list, regardless of the alphabet? The
Copy a List You cannot copy a list simply by typing There are ways to make a copy, one way is to use the built-in List method
Join Two Lists There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the
List Methods Python has a set of built-in methods that you can use on lists.
Below YouTube content is helpful for better understanding:
Tags: Dictionary List Append List Delete List Insert List Pop List Remove List Sort Python Python List Set Tuple | ||||||||||||||||||||||
| ||||||||||||||||||||||
| ||||||||||||||||||||||
|
OTHER POSTS IN THE SAME CATEGORYPython IteratorsPython InheritancePython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python String OperationsPython Data TypesPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python? |