Dictionary and Tuples Flashcards

1
Q

What is the syntax for creating a tuple?

A

my_tuple = (1, 2, 3).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create a single-element tuple?

A

Add a trailing comma: my_tuple = ('item',).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the key difference between a list and a tuple?

A

Tuples are immutable; lists are mutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to access an element in a tuple?

A

Use indexing: my_tuple[0] (e.g., (1,2,3)[1]2).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to slice a tuple?

A

Use slicing: my_tuple[start:end:step] (e.g., (1,2,3,4)[1:3](2,3)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What method counts occurrences of a value in a tuple?

A

.count(value) (e.g., (1,2,2).count(2)2).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What method returns the index of a value in a tuple?

A

.index(value) (e.g., (1,2,3).index(2)1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to concatenate two tuples?

A

Use + (e.g., (1,2) + (3,4)(1,2,3,4)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to repeat a tuple?

A

Use * (e.g., (1,2) * 2(1,2,1,2)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to convert a list to a tuple?

A

Use tuple(): tuple([1,2,3])(1,2,3).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to unpack a tuple?

A

Assign to variables: a, b = (1, 2)a=1, b=2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

question

A

answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax for creating a dictionary?

A

my_dict = {'key': 'value'}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to access a value in a dictionary?

A

Use the key: my_dict['key'] (e.g., {'a':1}['a']1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add or update a key-value pair?

A

Assign directly: my_dict['new_key'] = 'new_value'.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to safely access a value without raising KeyError?

A

Use .get(key) (e.g., my_dict.get('key')value or None).

17
Q

What method removes a key and returns its value?

A

.pop(key) (e.g., {'a':1}.pop('a')1).

18
Q

How to remove all items from a dictionary?

A

Use .clear(): my_dict.clear(){}.

19
Q

What method returns all keys in a dictionary?

A

.keys() (e.g., {'a':1}.keys()['a']).

20
Q

What method returns all values in a dictionary?

A

.values() (e.g., {'a':1}.values()[1]).

21
Q

What method returns key-value pairs as tuples?

A

.items() (e.g., {'a':1}.items()[('a',1)]).

22
Q

How to check if a key exists in a dictionary?

A

Use in: 'key' in my_dictTrue or False.

23
Q

How to merge two dictionaries?

A

Use .update(): {'a':1}.update({'b':2}){'a':1,'b':2}.

24
Q

How to create a dictionary from two lists?

A

Use zip(): dict(zip(keys, values)) (e.g., dict(zip(['a'],[1])){'a':1}).

25
Q

How to create a dictionary with default values?

A

Use defaultdict from collections or .setdefault().

26
Q

What is dictionary comprehension?

A

{k: v for k, v in iterable} (e.g., {x: x*2 for x in range(3)}{0:0,1:2,2:4}).

27
Q

How to copy a dictionary?

A

Use .copy(): new_dict = my_dict.copy().