Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Python List
List concatenation
Using the + operator, allows you to merge two or more lists into a single new list. It's like combining two shopping lists into one bigger list.
Here's how it works:
fruits = ["apple", "banana"] vegetables = ["carrot", "pea"] combined_list = fruits + vegetables print(combined_list) # Output: ["apple", "banana", "carrot", "pea"]
In this example, the + operator combines the elements of fruits and vegetables into a new list named combined_list. The order is preserved, with elements from fruits appearing first.
Key Points
- The + operator creates a new list. It doesn't modify the original lists.
- You can concatenate multiple lists at once using the same syntax.
list1 = [1, 2, 3] list2 = [4, 5] list3 = ["a", "b"] combined_list = list1 + list2 + list3 print(combined_list) # Output: [1, 2, 3, 4, 5, "a", "b"]
Things to consider
- Concatenation works with any lists containing compatible data types. Mixing lists with different data types (e.g., integers and strings) will result in a combined list with all elements.
- For specific use cases where you want to modify an existing list instead of creating a new one, consider using the extend() method. It appends elements from another list to the end of the original list.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What will be the output of the following code: fruits = ["apple", "banana"]; vegetables = ["carrot", "pea"]; combined_list = fruits + vegetables; print(combined_list)?
D["carrot", "pea"]
A["apple", "banana", "carrot", "pea"]
B["carrot", "pea", "apple", "banana"]
C["apple", "banana"]
Check Answer
Does the + operator modify the original lists when combining them?
ANo, it creates a new list.
BYes, it modifies the original lists.
CIt removes the original lists.
DIt copies the original lists.
Check Answer
What will combined_list = list1 + list2 + list3 produce if list1 = [1, 2, 3], list2 = [4, 5], list3 = ["a", "b"]?
D[1, 2, 3, 4, 5]
A[1, 2, 3, 4, 5, "a", "b"]
B[1, 2, 3, "a", "b", 4, 5]
C[1, 2, 3, 5, 4, "a", "b"]
Check Answer
What happens when you concatenate lists of different data types?
AAn error occurs.
BThe lists cannot be concatenated.
CIt creates a combined list with all elements.
DOnly compatible data types are combined.
Check Answer
Which method would you use to modify an existing list instead of creating a new one?
Cmerge()
Dadd()
Aappend()
Bextend()
Check Answer