Lists, Tuples, and Dictionaries
Welcome to this lesson on Python’s fundamental data structures: lists, tuples, and dictionaries. As a Java developer, you’re already familiar with arrays and HashMaps. In this lesson, we’ll explore Python’s equivalents and some unique features that make Python’s data structures powerful and flexible.
Lists: Python’s Dynamic Arrays
In Python, lists are similar to Java’s ArrayList. They’re dynamic, mutable, and can contain elements of different types.
# Python
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits[0]) # Output: apple
print(len(fruits)) # Output: 4
In Java, you might be used to:
// Java
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("date");
System.out.println(fruits.get(0)); // Output: apple
System.out.println(fruits.size()); // Output: 4
List Comprehensions
Python offers a powerful feature called list comprehensions, which allows you to create lists in a concise way:
# Python
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
This is equivalent to:
# Python
squares = []
for x in range(10):
squares.append(x**2)
Java doesn’t have a direct equivalent, but you might use streams to achieve something similar:
// Java
List<Integer> squares = IntStream.range(0, 10)
.map(x -> x * x)
.boxed()
.collect(Collectors.toList());
Tuples: Immutable Sequences
Tuples are immutable sequences in Python. They’re similar to lists but can’t be modified after creation.
# Python
coordinates = (3, 4)
print(coordinates[0]) # Output: 3
Java doesn’t have a direct equivalent to tuples, but you might use an immutable list or a custom class:
// Java
List<Integer> coordinates = List.of(3, 4);
System.out.println(coordinates.get(0)); // Output: 3
Dictionaries: Python’s HashMap
Dictionaries in Python are similar to Java’s HashMap. They store key-value pairs and offer fast lookups.
# Python
person = {
"name": "Alice",
"age": 30,
"city": "New York"
}
print(person["name"]) # Output: Alice
person["job"] = "Engineer" # Adding a new key-value pair
In Java, you might be familiar with:
// Java
Map<String, Object> person = new HashMap<>();
person.put("name", "Alice");
person.put("age", 30);
person.put("city", "New York");
System.out.println(person.get("name")); // Output: Alice
person.put("job", "Engineer"); // Adding a new key-value pair
Dictionary Comprehensions
Similar to list comprehensions, Python offers dictionary comprehensions:
# Python
squares_dict = {x: x**2 for x in range(5)}
print(squares_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Sets: Unordered Collections of Unique Elements
Python also has a built-in set type, which is an unordered collection of unique elements:
# Python
fruits_set = {"apple", "banana", "cherry"}
fruits_set.add("date")
print("banana" in fruits_set) # Output: True
In Java, you might use a HashSet:
// Java
Set<String> fruitsSet = new HashSet<>(Arrays.asList("apple", "banana", "cherry"));
fruitsSet.add("date");
System.out.println(fruitsSet.contains("banana")); // Output: true
Conclusion
In this lesson, we’ve explored Python’s core data structures: lists, tuples, dictionaries, and sets. We’ve seen how they compare to Java’s arrays, ArrayLists, HashMaps, and HashSets. We’ve also looked at some Python-specific features like list and dictionary comprehensions.
These data structures form the backbone of many Python programs, and understanding them well will significantly enhance your Python programming skills.
In the next lesson, we’ll dive into string manipulation and input/output operations in Python, comparing them with Java’s approaches. We’ll explore Python’s powerful string methods, f-strings for formatting, and how to handle file I/O operations.