- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Create and Assign Lists in Python?
We can Create and Assign List by Insert, Append Length, Index, Remove and Extend, etc..
Lists are mutable and the changeble object is enclosed within the square brackets i.e [ ], Lists in python is easy.
Example
list =["Tutorials ","Point", "Pvt","Ltd"] list
Output
['Tutorials ', 'Point', 'Pvt', 'Ltd']
Assign Values in Lists To assign values in lists, use square brackets
Example
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])
When the above code is executed, Following is the preceding output
Output
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
append() and extend() in python append add its argument/parameter as a single element at the end of a list. the list length will increase by one.
Example
list=['Red','Green','Yellow','Purple','White','Black'] list.append('Orange') print(list)
Output
['Red', 'Green', 'Yellow', 'Purple', 'White', 'Black', 'Orange']
extend() iterates over its parameter adding each element to the list and extending the list length of the list will increase through the iterable argument
Example
list=['Car','Bike','Scooty','Bus','Metro'] list.extend('Car') print(list)
When the above code is executed, Following is the preceding output
Output
['Car', 'Bike', 'Scooty', 'Bus', 'Metro', 'C', 'a', 'r']
Index: Index method can search easily, that expects a value is to be searched
Example
list=['Car','Bike','Scooty','Bus','Metro'] list.index('Scooty')
Output
2