Intorduction :-
A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas. As shown above, lists can contain elements of different types as well as duplicated elements.
- Scikit-Learn.
- Keras.
- Theano.
- SciPy. Automation Testing Python tools.
- Selenium.
- Robot Framework.
Tools :-
- List is a collection which is ordered and changeable.
- Tuple is a collection which is ordered and unchangeable.
- Set is a collection which is unordered, unchangeable*, and unindexed.
- Dictionary is a collection which is ordered** and changeable.
- insert()copy()index()count()sort() and reverse()pop() and remove()append() and extend().
Types :-
Syntax :-
1. list.append(obj): Appends object obj to list
2. list.count(obj): Returns count of how many times obj occurs in list
3. list.extend(seq): Appends the contents of seq to list
4. list.index(obj): Returns the lowest index in list that obj appears
5. list.insert(index, obj): Inserts object obj into list at offset index
6. list.pop(obj = list[-1]): Removes and returns last object or obj from list
7. list.remove(obj): Removes object obj from list
8. list.reverse(): Reverses objects of list in place
9. list.sort([func]): Sorts objects of list,
How it works?
In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
Why?
In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
- Artificial Intelligence.
- Data Science.
- Web Development.
- Machine Learning.
- Embedded Application.
- Game Development.
- Business Applications.
Trends :-
Benefits :-
First of all, you’re reducing 3 lines of code into one, which will be instantly recognizable to anyone who understands list comprehensions. Secondly, the second code is faster, as Python will allocate the list’s memory first, before adding the elements to it, instead of having to resize on runtime.
Example 1: Create lists from string, tuple, and list :-
- # empty list
- print(list())
- # vowel string
- vowel_string = ‘aeiou’
- print(list(vowel_string))
- # vowel tuple
- vowel_tuple = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
- print(list(vowel_tuple))
- # vowel list
- vowel_list = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
- print(list(vowel_list))
Output :-
- []
- [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
- [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
- [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Example 2: Create lists from set and dictionary
- # vowel set
- vowel_set = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}
- print(list(vowel_set))
- # vowel dictionary
- vowel_dictionary = {‘a’: 1, ‘e’: 2, ‘i’: 3, ‘o’:4, ‘u’:5}
- print(list(vowel_dictionary))
Output :
- [‘a’, ‘o’, ‘u’, ‘e’, ‘i’]
- [‘o’, ‘e’, ‘a’, ‘u’, ‘i’]
Example 3: Create a list from an iterator object :-
- # objects of this class are iterators
- class PowTwo:
- def __init__(self, max):
- self.max = max
- def __iter__(self):
- self.num = 0
- return self
- def __next__(self):
- if(self.num >= self.max):
- raise StopIteration
- result = 2 ** self.num
- self.num += 1
- return result
- pow_two = PowTwo(5)
- pow_two_iter = iter(pow_two)
- print(list(pow_two_iter))
Output :
- [1, 2, 4, 8, 16]
Conclusion :-
Understanding the syntax of Python is greaHow it workst and all, and Python by itself is indeed a great language, but the fundamentals of Python aren’t why Python is a successful language. There are plenty of fun-to-write languages that are just like Python, such as Ruby, Julia, even R.