Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62d4358

Browse files
added list slicing
1 parent 3912256 commit 62d4358

File tree

1 file changed

+134
-2
lines changed

1 file changed

+134
-2
lines changed

‎python3_helpful_coding_patterns_&_features.ipynb

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,144 @@
512512
"This solution has only one loop, and with the `get()` method being $O(1),ドル the total time complexity for this solution is $O(n)$."
513513
]
514514
},
515+
{
516+
"cell_type": "markdown",
517+
"metadata": {},
518+
"source": [
519+
"# Lists"
520+
]
521+
},
515522
{
516523
"cell_type": "code",
517-
"execution_count": null,
524+
"execution_count": 3,
518525
"metadata": {},
519526
"outputs": [],
520-
"source": []
527+
"source": [
528+
"my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']"
529+
]
530+
},
531+
{
532+
"cell_type": "markdown",
533+
"metadata": {},
534+
"source": [
535+
"## Access Every nth List Element"
536+
]
537+
},
538+
{
539+
"cell_type": "markdown",
540+
"metadata": {},
541+
"source": [
542+
"Access every element at an even index number:"
543+
]
544+
},
545+
{
546+
"cell_type": "code",
547+
"execution_count": 11,
548+
"metadata": {},
549+
"outputs": [
550+
{
551+
"data": {
552+
"text/plain": [
553+
"['two', 'four', 'six', 'eight']"
554+
]
555+
},
556+
"execution_count": 11,
557+
"metadata": {},
558+
"output_type": "execute_result"
559+
}
560+
],
561+
"source": [
562+
"my_list[1::2]"
563+
]
564+
},
565+
{
566+
"cell_type": "markdown",
567+
"metadata": {},
568+
"source": [
569+
"Access every element at an odd index number:"
570+
]
571+
},
572+
{
573+
"cell_type": "code",
574+
"execution_count": 13,
575+
"metadata": {},
576+
"outputs": [
577+
{
578+
"data": {
579+
"text/plain": [
580+
"['one', 'three', 'five', 'seven']"
581+
]
582+
},
583+
"execution_count": 13,
584+
"metadata": {},
585+
"output_type": "execute_result"
586+
}
587+
],
588+
"source": [
589+
"my_list[::2]"
590+
]
591+
},
592+
{
593+
"cell_type": "markdown",
594+
"metadata": {},
595+
"source": [
596+
"More generally, access every $n^{th}$ element from the list:"
597+
]
598+
},
599+
{
600+
"cell_type": "code",
601+
"execution_count": 14,
602+
"metadata": {},
603+
"outputs": [
604+
{
605+
"data": {
606+
"text/plain": [
607+
"['one', 'four', 'seven']"
608+
]
609+
},
610+
"execution_count": 14,
611+
"metadata": {},
612+
"output_type": "execute_result"
613+
}
614+
],
615+
"source": [
616+
"n = 3\n",
617+
"my_list[::n]"
618+
]
619+
},
620+
{
621+
"cell_type": "markdown",
622+
"metadata": {},
623+
"source": [
624+
"## List Reversal"
625+
]
626+
},
627+
{
628+
"cell_type": "markdown",
629+
"metadata": {},
630+
"source": [
631+
"This is an extension of the idea in the previous section. Use Python's list slicing feature, with `-1` causing a reversal in the direction of increments."
632+
]
633+
},
634+
{
635+
"cell_type": "code",
636+
"execution_count": 2,
637+
"metadata": {},
638+
"outputs": [
639+
{
640+
"data": {
641+
"text/plain": [
642+
"['five', 'four', 'three', 'two', 'one']"
643+
]
644+
},
645+
"execution_count": 2,
646+
"metadata": {},
647+
"output_type": "execute_result"
648+
}
649+
],
650+
"source": [
651+
"my_list[::-1]"
652+
]
521653
},
522654
{
523655
"cell_type": "code",

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /